Create a ContentType object on model save
Hello,
I have two models, 1) FlashCard and 2) Practice, and I have a generic relation
in the FlashCard model pointing to the Practice model using the contenttypes
framework.
The Practice model tracks information like how many times something was
practiced, and calculates an `easy factor'.
Each time I save a FlashCard I want at the same time to create Practice
object for the flashcard, automatically.
class Practice(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
item = generic.GenericForeignKey('content_type', 'object_id')
user = models.ForeignKey(User)
class FlashCard(models.Model):
"""
A basic Flashcard.
Has a front and a back view.
"""
front = models.TextField(
max_length = 255,
verbose_name = "Front")
back = models.TextField(
max_length = 255,
verbose_name = "Back")
user = models.ForeignKey(User)
practice = generic.GenericRelation(Practice)
I read the contenttypes documentation, and as far as I understand to create a
related Practice object for a flashcard instance I should do the following:
>>> user = User.objects.get(username="kenny")
# Create a sample flashcard
>>> flashcard = ("bla", "bla", user)
# ...and create a Practice object for it
>>> new_practice = Practice(item=flashcard)
# then I try to save it, but that fails with a large backtrace
>>> new_practice.save()
[snip]
Here the actual backtrace: http://dpaste.com/hold/272617/
Well, but this is not the reason I opened this thread. Still I want to create
Practice object for my FlashCard object.
I started a research on Google, but I really haven't found anything really
helping me to do that.
Can you guys give me a helping hand on this with your expertise, please?
Additional information about my environment:
- Django 1.2.3
- PostgreSQL 8.4.5
Cheers,
Kenny
--
- Kenny Meyer <knny.myer@gmail.com>
To understand recursion, we must first understand recursion.
--
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home