On 18 sep, 08:26, Axel Bock <mr.axel.b...@gmail.com> wrote:
> Hello,
>
> I am currently experimenting with a little webapp, and I have the
> following problem. I want to assign two properties of the same type to
> a class. (Stupid) Example: I have a meal, and it comes with two sides.
> The sides itself are another database key - they can change on a
> whim :) . It looks something like this:
>
> class Meal(models.Model):
>     side1 = models.ForeignKey('SidesList')
>     side2 = models.ForeignKey('SidesList')
>     # ....
>
> class SidesList(models.Model):
>     name = models.CharField(max_length=20)
>     availability = models.BooleanField()
>
> Now I get an error when compiling this.

s/compiling/importing/

> See, I need exactly two
> properties of the same type (side1, side2) in two _different_ object
> properties. The meal comes with exactly two sides, you see? :) And
> they must be distinguishable (something like post-main-course side",
> and "side with main course" out of the same pool).
>
> The error is: "Accessor for field 'side1' clashes with related field
> 'SidesList.leg_set'." (the same error for side2).
>
> Any help here? I'm quite new to dango, so a little hint would be
> nice :)

Django automatically build the "reverse" relationship from SideLists
(which should be renamed "Side" IMHO) to Meal, using the default
accessor name "meal_set" (ie : model_name.lower() + "_set"). Since you
have TWO foreignkeys, it tries to build 2 accessors, and of course it
can't build the second since you can't have two attributes with the
same name.

To make a long story short, you need to explicitely specifies the
related accessor name using the "related_name" argument in the
ForeignKey constructor as werefr0g suggested.

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

Reply via email to