|
nbv4 posted:Actually an abstract model can't be used here because the only use they have is prototyping other models. You can't call managers or anything else on abstract models. But a "multi-table" inheritance seem to work OK so far... Instinct says isinstance(some_music_obj, some_model_class) would work. Not sure if it's ideal though.
|
# ? Nov 23, 2008 23:33 |
|
|
# ? Jun 5, 2024 07:25 |
|
nbv4 posted:edit: If I run "music = Music.objects.all()", it returns a list of Music objects. How can I tell if a Music object is representing a CD or a record? I realize you can do "music.record.rotation_speed", but how can I tell which one it is? You should be able to check if music.record is None, music.cd is None, etc. pokeyman posted:Instinct says isinstance(some_music_obj, some_model_class) would work. Not sure if it's ideal though. Pretty sure this doesn't work, because the Python objects returned are instances of Music even if they represent a Record tuple in the database.
|
# ? Nov 24, 2008 06:48 |
|
pokeyman posted:Instinct says isinstance(some_music_obj, some_model_class) would work. Not sure if it's ideal though. I'm having the same problem right now. I can't do that within the parent model class because the children are declared later in the file . You also can't create a foreign key to the abstract models. Janitor Prime fucked around with this message at 06:51 on Nov 24, 2008 |
# ? Nov 24, 2008 06:48 |
|
MEAT TREAT posted:I'm having the same problem right now. I can't do that within the parent model class because the children are declared later in the file . Come again? If you need to check isinstance() in a method, that'll work no matter where in the file the child is declared. If you define subclass models, instances of the parent model should have an attribute named after the child that you can check. If in doubt, open a shell and instantiate some model objects, then poke at their attributes to see what each one defines.
|
# ? Nov 24, 2008 06:51 |
|
How do I go about doing this then?code:
|
# ? Nov 24, 2008 06:56 |
|
In the shell I can do stuff like this:code:
|
# ? Nov 24, 2008 07:22 |
|
This is ugly, but one of the downsides of an ORM is that you basically throw away many of the benefits from polymorphism. If anybody has a better way to do this, I'd be eager to see it.code:
|
# ? Nov 24, 2008 07:24 |
|
Janin posted:This is ugly, but one of the downsides of an ORM is that you basically throw away many of the benefits from polymorphism. If anybody has a better way to do this, I'd be eager to see it. Close but it gives me a "DoesNotExist: GameCharacter matching query does not exist." I tried it and it seems to work on the characters only. When I try to get the name of a monster, it give me that error. Which makes sense since that property self.gamecharacter doesn't exist. I'm just glad there is a somewhat sane solution to all this.
|
# ? Nov 24, 2008 07:38 |
|
code:
|
# ? Nov 24, 2008 07:53 |
|
Odd, perhaps that was on an older version. If you're going to catch the exceptions, there's no need to check for a non-None attribute.
|
# ? Nov 24, 2008 08:25 |
|
MEAT TREAT posted:
You could do: code:
|
# ? Nov 24, 2008 18:15 |
|
To the gentleman who wanted to have a view that combined querysets of different types that had a few common fields, I wrote a blog post about it today, hope it's helpful, here
|
# ? Nov 25, 2008 01:46 |
|
My seemingly decently elegant django.contrib.comments signal-based reCAPTCHA implementation, which I posted a link to earlier in this thread, suddenly got some serious egg on its face when someone pointed out that the comment framework's signal handling only allows for returning a HTTP 40x error when a signal handler returns false rather than redisplaying the form with a message. As a result, people mistyping the captcha get some seriously crappy feedback (or lack thereof). For reference, here's the link. Is there no way of handling this with the current framework without subclassing the comments form and implementing custom logic? I was hoping that wouldn't be necessary
|
# ? Dec 10, 2008 13:11 |
|
The Red Baron posted:My seemingly decently elegant django.contrib.comments signal-based reCAPTCHA implementation, which I posted a link to earlier in this thread, suddenly got some serious egg on its face when someone pointed out that the comment framework's signal handling only allows for returning a HTTP 40x error when a signal handler returns false rather than redisplaying the form with a message. As a result, people mistyping the captcha get some seriously crappy feedback (or lack thereof). For reference, here's the link. You could monkeypatch CommentForm.__new__ to apply a decorator on the security_errors function to check for recaptcha errors. deimos fucked around with this message at 18:16 on Dec 10, 2008 |
# ? Dec 10, 2008 18:13 |
|
deimos posted:You could monkeypatch CommentForm.__new__ to apply a decorator on the security_errors function to check for recaptcha errors. I've considered doing that, or monkeypatching comments.get_form(), but the problem is that I need to supply the the client's IP address to recaptcha, and doing that from a form object is tricky because as far as I know, form objects only have access to the request.POST data and not the request.META data. From the comments code I can't see any obvious ways of getting a hold of the request object from that context. Why can't django as a rule pass the request object in kwargs or something to functions that are obviously meant to be user-overridable? edit: would it be considered a horrible hack to wrap post_comment and send it a copy of the request object with an IP field injected into the POST map? The Red Baron fucked around with this message at 19:44 on Dec 10, 2008 |
# ? Dec 10, 2008 19:24 |
|
I have a model that has about 80 boolean values. Whats the Right Way to group these fields together? For instance, 12 of those boolean values are column1_hide, column2_hide, etc, which correspond to which columns the user wants to hide somewhere on the site. I'm going to define a function on the model that returns an array of each column the user wants hidden. If the user has column5_hide and column6_hide set as true, I want that function to return ["column5", "column6"]. I want to be able to iterate through all the columns in the model, instead of doing something like:code:
|
# ? Dec 16, 2008 00:52 |
|
nbv4 posted:I have a model that has about 80 boolean values. Whats the Right Way to group these fields together? For instance, 12 of those boolean values are column1_hide, column2_hide, etc, which correspond to which columns the user wants to hide somewhere on the site. I'm going to define a function on the model that returns an array of each column the user wants hidden. If the user has column5_hide and column6_hide set as true, I want that function to return ["column5", "column6"]. I want to be able to iterate through all the columns in the model, instead of doing something like: But if you just need some code to help do what you want with what you've got, this does the job: code:
|
# ? Dec 16, 2008 07:45 |
|
No Safe Word posted:Well first of all you probably shouldn't have a model with 80 boolean values, you should use a bitmask or some other way of storing all those booleans in one field that's not quite so cumbersome. Doesn't that still violate DRY? If you wanted to add an extra column, you'd have to change that 12 to a 13.
|
# ? Dec 16, 2008 19:06 |
|
nbv4 posted:Doesn't that still violate DRY? If you wanted to add an extra column, you'd have to change that 12 to a 13. DRY means "Don't Repeat Yourself" so it doesn't violate DRY but it does violate the "don't put hardcoded constants in code" best practice. And yeah, data model changes requiring code changes isn't that wacky of an idea in the first place. If you're changing your data model, chances are it's going to require a redeployment of the code anyway. If you want to get really hairy and ugly you can introspect the column names and do it that way but really the best option (in my opinion) is to not have a data model like that.
|
# ? Dec 16, 2008 20:44 |
|
After some unsuccessful Google-ing, I figured I would turn to the experts. A friend and I are looking to develop a turn-based strategy web-game (just for fun and practice). I have been playing around with Django at work lately, and am wondering if it would make a good platform for this. I know how to make traditional sites with it; however, I am stumped with where to begin on this. I'm not sure how the handle the time-based (turns, ticks, whatever) updates to the game database, and where in the application I should handle the game logic. Do I need some kind of separate process or application? Any advice would be greatly appreciated. Thanks.
|
# ? Dec 16, 2008 21:03 |
|
Funny you should ask since I just finished my first Django project which was a turn based game. It was for an OOP course where good design was more important than if it actually worked. It was a lot of fun learning python and django, thankfully we came up with a good design (I think) for it. Our project was for D&D and this is how we handled the turns. I've removed all the irrelevant parts from it. First we defined a Turn model which did nothing other than store whose turn it was and what actions they had already taken. We then created an Encounter model which had all the characters and all the monsters in it. Finally we had an Initiative model which had 2 foreign keys and the actual initiative score, one FK was the character/monster and the other the encounter. With that established we had a few methods in the Encounter model, one to start the combat and one to find the next highest initiative and set the turn to that combatant. The start combat method just created the initiatives for each combatant, and set the turn to the highest initiative. From here we created a view for the players where they could choose to finish their turn and a view for the DM to start the encounter. In the templates we used polling and AJAX to inform the next player that it was his turn and he could proceed to take actions as normal. Obviously if someone tried to take an action out of turn the view wouldn't allow them. That is the jist of how we made our game work, there are lots of things you could do to change it up, like setting up a timer that would automatically call the next turn if the player doesn't respond after a set amount of time.
|
# ? Dec 17, 2008 10:46 |
|
I am trying to make an application for recipes using Django. I am starting simple and going from the tutorial but not getting the results I want. In a less strict framework I'd be able to represent what I want to easily but I'm having trouble figuring out how to do it the Django way while keeping the database clean and easy to query to prevent performance issues. For this example, let's say I have two different models. Recipe, and Ingredient. A recipe has a name and a set of ingredients and an amount and measurement corresponding to each ingredient. An ingredient has only a name. Here's an example schema of what the database in my mind would look like in 3NF: code:
I'm having trouble figuring out how to get Django to recreate this database structure. The tutorial example with the poll application is similar but I'm not happy with the database schema because you can't use the same choice in multiple polls. This means that if you had 50 yes/no polls, you'd have "Yes" and "No" replicated in the database 50 times which, frankly, sucks. I think I need to use a ManyToManyField for this but I'm having trouble finagling it to be the way I want. Here's what I have right now but I can't figure out where to put the amount and unit for the ingredients: code:
|
# ? Dec 25, 2008 20:19 |
|
ashgromnies posted:I am trying to make an application for recipes using Django. I am starting simple and going from the tutorial but not getting the results I want. In a less strict framework I'd be able to represent what I want to easily but I'm having trouble figuring out how to do it the Django way while keeping the database clean and easy to query to prevent performance issues. code:
edit: vvvv well hot drat, those Django guys are pretty smart vvvv No Safe Word fucked around with this message at 22:31 on Dec 25, 2008 |
# ? Dec 25, 2008 21:36 |
|
ashgromnies, I think you're looking for this: http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships
|
# ? Dec 25, 2008 21:49 |
|
I'm trying to make a checkers game using Django. My main goal here is to find a way to make a game grid, which I could then use for other games. Here is my model code for the board. It's kind of an ugly mess, and it uses a big-rear end list literal to build the board. I'm pretty sure this is a horrible way to do it. It's not DRY or flexible at all, there's no way in hell I'm going to be able to save this to a database, and moving things around on this board is going to be a pain in the rear end. How the hell do you implement something like a game board? Surely I'm not the first person to try this, but even looking through PyGame articles I haven't managed to find anything. EDIT: Okay, so I came up with a sort of ultra-simple mini-DSL for building game grids, it's a lot shorter and less ugly than my last try: chess/boards/functions.py code:
code:
I still don't like the fact that it's implemented as a list of lists. At least now it can be more easily changed if I come up with a better idea. And yeah, the project is called 'chess'. I scaled down the complexity early on, and never renamed it. Gone Fission fucked around with this message at 07:12 on Dec 27, 2008 |
# ? Dec 27, 2008 05:43 |
|
So Adrian and company are working on an updated Django Book, and the work-in-progress is online for everyone to see like last time, yay! http://www.djangobook.com/en/2.0/ (Django Book 2.0 covers Django 1.0 )
|
# ? Jan 9, 2009 16:33 |
|
No Safe Word posted:So Adrian and company are working on an updated Django Book, and the work-in-progress is online for everyone to see like last time, yay! James Bennet also announced(officially) that he's working on a 2.0 of Practical Django Projects. Also, for those who haven't been following 1.1, there is going to be a crapton of new, awesome, stuff, I've been helping out with some of it and it's going to be awesome
|
# ? Jan 9, 2009 17:05 |
|
What're the best additions in 1.1, in your opinion?
|
# ? Jan 9, 2009 17:43 |
|
Well aggregation is really nice, I love the API on it. Model validation is going to be a huge boon for people who want it just because there wasn't an easy workaround for those that needed it before(you could always write raw SQL). Plus bulk-editing stuff on the changelist actually looks great and can be a huge help for what should be simple tasks(for example making a BooleanField editable from there and just using it to enable stuff en masse). Of course I've been following all of these, coded 1 myself, and provided a bit of assistance on the others, so I might be biased
|
# ? Jan 9, 2009 17:52 |
|
Any idea if the read-only admin fields are going to make it in?
|
# ? Jan 9, 2009 21:04 |
|
mwarkentin posted:Any idea if the read-only admin fields are going to make it in? Possible, it's already possible to do in your own code though: http://lazypython.blogspot.com/2008/12/building-read-only-field-in-django.html
|
# ? Jan 9, 2009 21:50 |
|
Is there any cool way to push notifications from a Django app to prospective clients? Constantly polling seems kind of silly, but I can't think of a more effective way to do it. Something like the D&D guy above, he just polled to see when it was the player's turn. That's fine, but is there some crazy way he could've pushed a notification out from the server for that?
|
# ? Jan 12, 2009 09:15 |
|
pokeyman posted:Is there any cool way to push notifications from a Django app to prospective clients? Constantly polling seems kind of silly, but I can't think of a more effective way to do it. Something like the D&D guy above, he just polled to see when it was the player's turn. That's fine, but is there some crazy way he could've pushed a notification out from the server for that? What I discovered was that HTTP is a client oriented protocol, that means that the only way for a server to communicate with a client is by responding to a request that they make. Look into Comet. According to that page the most common way is to use AJAX with long polling.
|
# ? Jan 12, 2009 11:16 |
|
FYI aggregates: http://code.djangoproject.com/changeset/9742
|
# ? Jan 15, 2009 17:35 |
|
Has anybody had any luck extending the django.contrib.auth User model in their apps? I've been following the instructions on this blog post, but now when I try to login to the admin interface it's failing. (note: this is with Django 1.0.2 final) If I fire up the manage.py shell, and I do: code:
admin app error message posted:Please enter a correct username and password. Note that both fields are case-sensitive. So I did a bit of digging: code:
|
# ? Jan 17, 2009 03:52 |
|
No Safe Word posted:Has anybody had any luck extending the django.contrib.auth User model in their apps? I've been following the instructions on this blog post, but now when I try to login to the admin interface it's failing. How are you starting the server? 'runserver' retains your db, 'testserver' recreates everything.
|
# ? Jan 30, 2009 06:08 |
|
For those who are interested EuroDjangoCon registration is now open here: http://euro.djangocon.org/ . I've submitted a talk proposal on some advanced forms techniques.
|
# ? Feb 11, 2009 21:10 |
|
How do I get django to recognize packages for python? I installed mechanize (http://wwwsearch.sourceforge.net/mechanize/) and django can't seem to import it. Or can I just get around this completely by using something within django to scrape imdb pages? edit: I installed mechanized by running the setup.py, but there were also alternate instructions to just copy files into the Lib/site-packages directory. I did that and it worked. I'm guessing the setup.py installed it somewhere where only python could find it. I guess I now have two copies of mechanized floating around. Could anyone explain what happened? Crazak P fucked around with this message at 09:07 on Feb 14, 2009 |
# ? Feb 14, 2009 08:50 |
|
Crazak P posted:How do I get django to recognize packages for python? Unless you're doing something really funky with your PythonPath when Django loads up, Django is just Python, and should always have full access to anything on your global PythonPath. Given what you said about lib/site-packages, I'm guessing that you may have 2 Python versions set up? That, or some other aspect of the configuration is messed up; typically, a python setup.py install SHOULD put stuff into your site-packages directory, and site-packages should always be on your PythonPath (and it sounds like it is). Long story short: tell us exactly what OS you're using, what exact version you're on, and if possible, what version(s) of Python you've got and how they were installed (e.g. via MacPorts, via apt-get, from source, whatever). That should help figure out what's going on.
|
# ? Feb 14, 2009 17:21 |
|
|
# ? Jun 5, 2024 07:25 |
|
I've searched for information on this, but can't really seem to find anything useful. I think my Google-fu has gotten weak. What is the best way to run something automatically every so often? I want to go through my database and update it every 15 minutes or so. Is there any way to do this from within the main Django application, or does it need to be a seperate process or cron job? And finally, what is the best method for locking the database while the update is running so anybody accessing the web interface isn't going to receive partially updated data? Thanks!
|
# ? Feb 14, 2009 19:43 |