On Sep 16, 5:49 pm, Steven <[EMAIL PROTECTED]> wrote:
> I just got started with Django (I'm using v1.0).. and have been trying
> (unsuccessfully) to add this line to my first model "author =
> models.ForeignKey(User)"... (see my models.py below).  Everytime I do
> so and sync I get the following message:
>
> ^[[31;1mError: One or more models did not validate:
> ^[[31;1mauth.message: Accessor for field 'user' clashes with related
> field 'User.message_set'. Add \
> a related_name argument to the definition for 'user'.
> ^[[0m^[[31;1mmusables.message: Accessor for field 'author' clashes
> with related field 'User.message\
> _set'. Add a related_name argument to the definition for 'author'.
> ^[[0m
>
> The odd thing is I decided to copy my model (see below it is called
> MessageV2) and then it worked fine?  Is there something "magical"
> about the first model you define in a file?  Is Django storing some
> extra state somewhere.. btw.. i've been "DROP TABLE"ing all my tables
> before manage.py syncdb ?
>
> Steven
>
> ===== models.py ======
>
> from django.db import models
> from django.contrib.auth.models import User
>
> # Create your models here.
>
> class Message(models.Model):
>     sent_date = models.DateTimeField()
>     subject = models.CharField(max_length = 100)
>     body = models.TextField()
>     slug = models.SlugField(max_length = 100)
>     # author = models.ForeignKey(User)  # uncommenting this causes the
> problem
>     def __unicode__(self):
>         return self.subject
>
> class MessageV2(models.Model):
>     sent_date = models.DateTimeField()
>     subject = models.CharField(max_length = 100)
>     body = models.TextField()
>     slug = models.SlugField(max_length = 100)
>     author = models.ForeignKey(User)  # this is where things are
> confusing
>
>     def __unicode__(self):
>         return self.subject

No, there's no magic, and the problem isn't to do with the position in
the file - it's to do with the name of your models.

As the error message states, the problem is with a name clash. When
you define a ForeignKey, Django automatically creates a reverse
relationship from the target model back to your original model. By
default, this is given the name of your model, which in your case is
Message, plus '_set' - ie message_set. However, User already has a
relation with a model called Message, which is part of the
contrib.auth application, so there's a clash.

The error message also helpfully states the solution: add a
related_name to the author definition. If you do
author = models.ForeignKey(User, related_name='author_messages')
things should work, and you'll be able to refer backwards from user to
authors via user.author_messages.all() and so on.

See the fine documentation here:
http://docs.djangoproject.com/en/dev/ref/models/fields/#foreign-key-arguments

--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to