|
Suspicious Dish posted:This is a bad idea. You're removing the big benefit of having a database keep all the data and build relationships. What if there are actually two separate Daniel Radcliffes that are actors? It's not uncommon to have two different actors with the same name. http://en.wikipedia.org/wiki/Screen_Actors_Guild#Unique_stage_names You could also just use a primary_key, or make the name primary (which would force things like Daniel Radcliffe (II) if there were more than one). And building up individual Cast entries per movie is literally no different; if he wants to pull a list of all roles Daniel Radcliffe has been in, he still has to do a search through all of Cast for name="Daniel Radcliffe" -- which would run in to the very same problem you have said occurs with my version.
|
# ? May 29, 2012 20:23 |
|
|
# ? Jun 8, 2024 08:10 |
|
The rule has been relaxed considerably, and not all productions require actors to be in the Screen Actors Guild, especially independent productions. There's also no similar rule for any of the unions of production crew members. Sparta posted:And building up individual Cast entries per movie is literally no different; if he wants to pull a list of all roles Daniel Radcliffe has been in, he still has to do a search through all of Cast for name="Daniel Radcliffe" -- which would run in to the very same problem you have said occurs with my version. Not quite. If he knows that Daniel Radcliffe acted in Harry Potter, he can find that, and use the Foreign Key associated with Harry Potter to find the others. If you ever want to attach any kind of other information to a cast or crew member (date of birth, bio), it's better to have it in one place.
|
# ? May 29, 2012 20:36 |
|
Suspicious Dish posted:The rule has been relaxed considerably, and not all productions require actors to be in the Screen Actors Guild, especially independent productions. There's also no similar rule for any of the unions of production crew members. Suspicious Dish posted:Not quite. If he knows that Daniel Radcliffe acted in Harry Potter, he can find that, and use the Foreign Key associated with Harry Potter to find the others. If you ever want to attach any kind of other information to a cast or crew member (date of birth, bio), it's better to have it in one place. He can find the other cast of HP, but finding the roles of Daniel Radcliffe would be difficult, as it would mean cycling through every Movie->Cast->where name="Daniel Radcliffe", versus just going Cast name=DR. Frankly, there should just be an Actor class (which is what I was sort of getting at). Have all the bio stuff in the Actor class, have a ManyToMany relationship in the Movies class referencing Actors. No need to make a separate Cast class.
|
# ? May 29, 2012 21:22 |
|
Why isn't any one suggesting a custom table for the ManyToManyField relationship? https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships Python code:
Python code:
|
# ? May 30, 2012 00:57 |
|
Every time I've ever tried to use a through table for a ManyToManyField field its ended in utter disaster. Enjoy watching your entire database implode when you try to delete one line in the through table!
|
# ? May 30, 2012 01:22 |
|
I've personally had no dramas. I mean, all you're doing is just being specific, Django creates a middleman table anyway for a many to many.
|
# ? May 30, 2012 01:39 |
|
M2M relations are utterly trivial. If you can't do those in Django, you should probably try writing them, until you get them right.
|
# ? May 30, 2012 10:51 |
|
ufarn posted:M2M relations are utterly trivial. If you can't do those in Django, you should probably try writing them, until you get them right. The problem for me is that each item you enter into an M2M needs a PK, which means it needs to be saved. That means that there's a mess of worms where I have to make sure I have all my data before I can enter it into the M2M or enter it into the database, and... well, the code I have really now is really messy, revolving around transactions. And that doesn't work because Django's support for @commit_manually is iffy, so I have to hack around it.
|
# ? May 30, 2012 12:11 |
|
The way I see it, if you are writing a reasonably complicated application you should probably do some work to abstract away all your work with the ORM. The time I spent doing this on the app I'm working with has made my view logic a breeze. Granted I had to do it if I wanted my do / undo stuff and queueing to work, but it really does simplify things for me if you break down every unit of database work into well tested methods that do the heavy lifting for you. Of course this has the plus side of down the track, making migrating away from Django's ORM to maybe SQLAlchemy somewhat less painful if it becomes necessary.
|
# ? May 30, 2012 12:33 |
|
ufarn posted:M2M relations are utterly trivial. If you can't do those in Django, you should probably try writing them, until you get them right. The hard part for me is just figuring out when to use them vs a foreign key as in deciding if I have a one-to-many or a many-to-many. I don't have a DBA background, but Django is so fun to work with.
|
# ? May 30, 2012 16:05 |
|
baxate posted:The hard part for me is just figuring out when to use them vs a foreign key as in deciding if I have a one-to-many or a many-to-many. I don't have a DBA background, but Django is so fun to work with. It's been a while, since I created a new model layer, but I don't think you ever use explicit one-to-many relations in Django. So it's usually just about using a Foreign Key versus M2M.
|
# ? May 30, 2012 16:13 |
|
Lamacq posted:Thanks to Twitter Bootstrap, most of my web apps look like this too Hah. Yeah, my intranet sites are pretty much bootstrap bootstrap bootstrap now. Its like a loving ron paul campagn, but with unicode crashes!
|
# ? May 30, 2012 17:35 |
|
Suspicious Dish posted:The problem for me is that each item you enter into an M2M needs a PK, which means it needs to be saved. That means that there's a mess of worms where I have to make sure I have all my data before I can enter it into the M2M or enter it into the database, and... This is relational theory 101 not just django, it's proper database design. M2M is trivial once you understand proper normalization. An ORM is not an excuse to not know how relational databases work. In order to make it easier and not have to fight the ORM the few times you may have to you can always play around with ForeignKey.on_delete and/or setting null=True. e: null=True is not something you actually want to do if you want to maintain referential integrity, just threw it out there so that you can learn, the hard way when your entire database becomes a nightmare of potholed crap data, that referential integrity is important deimos fucked around with this message at 22:10 on May 30, 2012 |
# ? May 30, 2012 22:04 |
|
deimos posted:This is relational theory 101 not just django, it's proper database design. M2M is trivial once you understand proper normalization. An ORM is not an excuse to not know how relational databases work. I want to do validation on my model before I enter it into the database. The M2M keys are also part of that validation process. That means I have to enter it into the database, add M2Ms, and then roll back the transaction if it fails validation. Unfortunately, due to commit_manually making sure the DB is clean after committing, I get a 500 error when I return from my view because a context processor just simply ran a select query, so I have to force it clean, but that's another issue.
|
# ? May 30, 2012 22:25 |
|
Suspicious Dish posted:Unfortunately, due to commit_manually making sure the DB is clean after committing, I get a 500 error when I return from my view because a context processor just simply ran a select query, so I have to force it clean, but that's another issue. Isn't this solved by changing the middleware order? e: middleware != context processor Err, this is your problem because you're using commit_manually as a decorator on the whole view and not taking into account querying context processors on the render call. You have two options there, abstract the transactional database manipulation into it's own method with the commit_manually decorator or use commit_manually as a context manager for the block of code that is transactional. edit2: another option is to simply commit/rollback after render_to_response, so this: code:
code:
Yet another option is, in your context processors to do something like this: code:
deimos fucked around with this message at 23:22 on May 30, 2012 |
# ? May 30, 2012 22:44 |
|
I'm doing option 2, only replace the commit() with set_clean().
|
# ? May 31, 2012 00:01 |
|
Suspicious Dish posted:I'm doing option 2, only replace the commit() with set_clean(). Yeah, I feel it's a better approach to use one of the first 2 options (both are basically: don't use the decorator on the entire view).
|
# ? May 31, 2012 00:24 |
|
ufarn posted:M2M relations are utterly trivial. If you can't do those in Django, you should probably try writing them, until you get them right. The problem has nothing to do with M2M relations, but rather is because of Django's idiotic ON DELETE CASCADE feature. Deleting a row in a through table thus causes all kinds of chaos unless you know how to explicitly step around it.
|
# ? May 31, 2012 00:25 |
|
German Joey posted:The problem has nothing to do with M2M relations, but rather is because of Django's idiotic ON DELETE CASCADE feature. Deleting a row in a through table thus causes all kinds of chaos unless you know how to explicitly step around it. Hello friend, may I interest you in ForeignKey.on_delete?
|
# ? May 31, 2012 00:27 |
I have a complicated SQL statement which I'm not sure can be made with Django. The simplest form I can get is: SELECT * FROM main_pic WHERE main_pic.id IN (SELECT pic_id FROM main_tag WHERE main_tag.name='nsfw'); For extra credit, each tag has a set of votes which indicate accuracy. SELECT * FROM main_pic WHERE main_pic.id IN (SELECT pic_id FROM main_tag WHERE (SELECT SUM(main_vote.direction) FROM main_vote AS score WHERE main_vote.target=main_tag.id) AND score > 0) I think. The other is a Hamming distance question which I asked in the SQL thread, but I think can be rewritten in the select through clever use of ORDER_BY. My question is, can I select pics from my database such that there is no 'nsfw' tag associated where the nsfw tag has more than 50% support in Django? Or do I have to use raw sql? EDIT: Missing 'AND'. Jo fucked around with this message at 00:35 on May 31, 2012 |
|
# ? May 31, 2012 00:33 |
|
What you want is pretty trivial actually. All you need is to link your main_pic to main_tag via a Foreign Key with a related name. code:
code:
https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.related_name You'll need to use an aggregate to do your (own) extra credit. https://docs.djangoproject.com/en/dev/topics/db/aggregation/ As for your third question, I have no loving idea what you are asking lol. Why would an nsfw tag have support in Django? Are you asking if you can count if 50% of your pics have a nsfw tag? You can do that with an aggregate. German Joey fucked around with this message at 00:59 on May 31, 2012 |
# ? May 31, 2012 00:56 |
German Joey posted:What you want is pretty trivial actually. All you need is to link your main_pic to main_tag via a Foreign Key with a related name. Thank you. That makes a lot of sense. I wasn't clear on the Hamming Distance function, but I'll come back and worry about it later.
|
|
# ? May 31, 2012 00:59 |
|
That doesn't seem to be what you want, as a picture can have many tags.Jo posted:SELECT * FROM main_pic WHERE main_pic.id IN (SELECT pic_id FROM main_tag WHERE main_tag.name='nsfw'); That seems to be a poor man's join: code:
code:
|
# ? May 31, 2012 01:44 |
Suspicious Dish posted:That doesn't seem to be what you want, as a picture can have many tags. This is closer to the model I have, and more agreeable to my intuition about the problem. I had it just selecting the ten most recent uploads, which I think got me locked into thinking about it in terms of operating on Pic. Makes WAY more sense to select by Tags, so long as I can enforce uniqueness and order. (Should be trivial, right?) To keep people from guessing, I'm going to dump my models below (unnecessary bits mostly excluded). code:
|
|
# ? May 31, 2012 05:33 |
|
get_tags can be replaced with related_name as well. By default, the related name is "%(class)s_set", so pic.tag_set. So to find all the pictures with 'nsfw' tags, you would do:code:
code:
Also, drop those semicolons, and remove the tabs. PEP8 compliance is a good thing to have.
|
# ? May 31, 2012 05:45 |
Thanks again for the help, everyone. Posting the solution here for anyone who stumbles on this by Google or is reading the thread.code:
code:
EDIT: Answering a question about why there are null images, replies to main posts are not required to have an image in them. This allows headless comments. I had them as separate entities at one point, but it proved actively detrimental to the feel of the site and made the handling code messy. I might reincorporate that in the future, but for now this approach works quite well. Jo fucked around with this message at 21:47 on May 31, 2012 |
|
# ? May 31, 2012 21:11 |
|
Uh, why do you have NULL/blank entries in your database? You should probably just get rid of those. Also, I'm not sure what you need to do. What's your actual question?
|
# ? May 31, 2012 21:22 |
Suspicious Dish posted:Uh, why do you have NULL/blank entries in your database? You should probably just get rid of those. I'm just posting an update/thanks in case someone else stumbles across the same question. I hate finding threads where someone has the exact problem I do, only to see them reply with, "Nvm, fixed it." and nothing more. Thanks again for your help.
|
|
# ? May 31, 2012 21:23 |
|
Jo posted:
code:
|
# ? Jun 1, 2012 14:01 |
|
Or like so: code:
|
# ? Jun 1, 2012 15:12 |
|
I'm quite sure you can chain multiple excludes together:code:
|
# ? Jun 1, 2012 15:36 |
|
Suspicious Dish posted:I'm quite sure you can chain multiple excludes together: This would exclude only rows with ALL of those values matching, and considering the img__isnull and img__exact I suspect this means no rows will ever be excluded. Excluding by chaining the exclude command on the queryset excludes with an OR clause
|
# ? Jun 1, 2012 16:19 |
|
Hm, I thought filter AND'd by default and exclude OR'd by default. I guess you could use a Q object, but that's getting a bit too fancy.
|
# ? Jun 1, 2012 17:22 |
|
This is how I would do it: Python code:
|
# ? Jun 1, 2012 18:08 |
|
how!! posted:This is how I would do it: Ah, good thinking about the () around it all. I wonder why I never though about that.
|
# ? Jun 1, 2012 20:05 |
Sharktopus posted:yeah then the two model approach should work just fine. Thanks a bunch for your patience and help. The aggregation querysets link is going to be super helpful for what I'm looking to do. For those of you using twitter bootstrap, are there any must-have django toolkits that make development easier, specifically forms?
|
|
# ? Jun 2, 2012 03:55 |
|
KingNastidon posted:Thanks a bunch for your patience and help. The aggregation querysets link is going to be super helpful for what I'm looking to do. I've been using https://github.com/maraujop/django-crispy-forms for my forms since forever.
|
# ? Jun 2, 2012 04:49 |
Maluco Marinero posted:Why isn't any one suggesting a custom table for the ManyToManyField relationship? Thanks! This worked perfectly well until I realized that there are people who act and direct. I changed it to this: Python code:
lunar detritus fucked around with this message at 05:58 on Jun 3, 2012 |
|
# ? Jun 2, 2012 21:43 |
|
I'm working on a new, simpler project since I have realized I understand much less than I thought I did. It's going to be a redirect-type service, but I need people to be able to add a couple different binary or choice switches; and I'd like them to be able to add a bunch. So, the linkmodel will have a link -> how would I represent a bunch of scaleable options? For instance, someone puts in link xyz.com and they'd like to have three switches (a= true or false, b= true or false, c= 1, 2, or 3). They are not telling me what they want these switches to be, but rather that those choices should exist. I will then make link variations from that info which would look like: link a:true, b:true, c:1 link a:true, b:true, c:2 link a:true, b:true, c:3 link a:true, b:false, c:1 link a:true, b:false, c:2 link a:true, b:false, c:3 link a:false, b:true, c:1 link a:false, b:true, c:2 link a:false, b:true, c:3 link a:false, b:false, c:1 link a:false, b:false, c:2 link a:false, b:false, c:3 However, I don't know what these switches are beforehand, and I don't know how many they want (probably up to 10 or 20, but I'd like to make it without a limit). I can't figure out how I'd represent this. Any help?
|
# ? Jun 2, 2012 22:36 |
|
|
# ? Jun 8, 2024 08:10 |
|
Sparta posted:I can't figure out how I'd represent this. Any help? Do you mean how would you represent this within python or how would you take user input for this?
|
# ? Jun 3, 2012 04:39 |