RE: ManyToMany problem
I got it! It turns out I was right in my suspicion. It doesn’t look like you can use the string notation to reference a model in a different module. Is that expected? Is there any way to do that? Basically, I simply moved the RestaurantHoodMap into the hood.py file, changed the string name to “RestaurantHoodMap”, and viola, it works. Can anyone explain why the string notation won’t work for a model in a different module? Thanks! -Jamie *From:* James DeMichele [mailto:james.demich...@redfin.com] *Sent:* Thursday, September 29, 2011 12:34 PM *To:* django-users@googlegroups.com *Subject:* Re: ManyToMany problem Thanks, Tom. So, I've changed my code slightly.but I'm still getting an error. I'm pretty much following exactly what is in the Django docs as well. I'm wondering if it's because I have my models in different modules. That shouldn't matter though should it? Maybe I'm doing something wrong with the string syntax. More help would be appreciated. Updated files: *models/hoods.py* class Hood(AbstractDateModel): name = models.CharField(unique=True) restaurants=models.ManyToManyField(Restaurant, through='restaurant_hood_map.RestaurantHoodMap') class Meta: db_table = "hoods" managed=False app_label="delivery" *models/restaurants.py* class Restaurant(AbstractDateModel): name=models.ForeignKey(CompanyName) class Meta: db_table="restaurants" managed=False app_label="delivery" *models/restaurant_hood_map.py* class RestaurantHoodMap(models.Model): restaurant = models.ForeignKey(Restaurant) hood = models.ForeignKey(Hood) class Meta: db_table="restaurant_hood_map" managed=False app_label="delivery" .Ok, so that's exactly what is in the Django Doc: https://docs.djangoproject.com/en/1.3/topics/db/models/#intermediary-manytomany However, here's the error I am getting this thing at the command line: >>> from models.restaurant_hood_map import RestaurantHoodMap >>> RestaurantHoodMap.objects.all() [] >>> RestaurantHoodMap.objects.all()[0] >>> RestaurantHoodMap.objects.all()[0].hood >>> RestaurantHoodMap.objects.all()[0].hood.name u'Westlands' >>> RestaurantHoodMap.objects.all()[0].hood.restaurants Traceback (most recent call last): File "", line 1, in File "C:\Python\lib\site-packages\django\db\models\fields\related.py", line 722, in __get__ RelatedManager = create_many_related_manager(superclass, self.field.rel) File "C:\Python\lib\site-packages\django\db\models\fields\related.py", line 478, in create_many_related_manager class ManyRelatedManager(superclass): File "C:\Python\lib\site-packages\django\db\models\fields\related.py", line 501, in ManyRelatedManager if rel.through._meta.auto_created: AttributeError: 'str' object has no attribute '_meta' Please help if you can! Thanks. -Jamie On Thu, Sep 29, 2011 at 11:38 AM, Tom Evans <tevans...@googlemail.com> wrote: On Thu, Sep 29, 2011 at 5:20 PM, James DeMichele <james.demich...@redfin.com> wrote: > Thanks for the response, that's a completely inaccurate statement. > > Here's an example from the Django docs: > > https://docs.djangoproject.com/en/1.3/topics/db/models/#intermediary-manyt > omany > > The Group object has through="Membership". > > Although, one thing I notice is that it looks like in the Django example > it is only using 1 ManyToMany field (e.g. the Person model does not have a > manytomany field). Is that required? Only one of the related items can > have a ManyToMany declared? > > That wouldn't make a lot of sense though, right? I would want to ask for > all Restaurants a Hood asand vice versa. I'd want all of the Hoods > that belong to a Restaurant. > You only place it on one side of the relationship. Django magic adds the appropriate methods to the other side of the relationship. DRY - Dont Repeat Yourself. Cheers Tom -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: ManyToMany problem
Thanks, Tom. So, I've changed my code slightly.but I'm still getting an error. I'm pretty much following exactly what is in the Django docs as well. I'm wondering if it's because I have my models in different modules. That shouldn't matter though should it? Maybe I'm doing something wrong with the string syntax. More help would be appreciated. Updated files: *models/hoods.py* class Hood(AbstractDateModel): name = models.CharField(unique=True) restaurants=models.ManyToManyField(Restaurant, through='restaurant_hood_map.RestaurantHoodMap') class Meta: db_table = "hoods" managed=False app_label="delivery" *models/restaurants.py* class Restaurant(AbstractDateModel): name=models.ForeignKey(CompanyName) class Meta: db_table="restaurants" managed=False app_label="delivery" *models/restaurant_hood_map.py* class RestaurantHoodMap(models.Model): restaurant = models.ForeignKey(Restaurant) hood = models.ForeignKey(Hood) class Meta: db_table="restaurant_hood_map" managed=False app_label="delivery" .Ok, so that's exactly what is in the Django Doc: https://docs.djangoproject.com/en/1.3/topics/db/models/#intermediary-manytomany However, here's the error I am getting this thing at the command line: >>> from models.restaurant_hood_map import RestaurantHoodMap >>> RestaurantHoodMap.objects.all() [] >>> RestaurantHoodMap.objects.all()[0] >>> RestaurantHoodMap.objects.all()[0].hood >>> RestaurantHoodMap.objects.all()[0].hood.name u'Westlands' >>> RestaurantHoodMap.objects.all()[0].hood.restaurants Traceback (most recent call last): File "", line 1, in File "C:\Python\lib\site-packages\django\db\models\fields\related.py", line 722, in __get__ RelatedManager = create_many_related_manager(superclass, self.field.rel) File "C:\Python\lib\site-packages\django\db\models\fields\related.py", line 478, in create_many_related_manager class ManyRelatedManager(superclass): File "C:\Python\lib\site-packages\django\db\models\fields\related.py", line 501, in ManyRelatedManager if rel.through._meta.auto_created: AttributeError: 'str' object has no attribute '_meta' Please help if you can! Thanks. -Jamie On Thu, Sep 29, 2011 at 11:38 AM, Tom Evanswrote: > On Thu, Sep 29, 2011 at 5:20 PM, James DeMichele > wrote: > > Thanks for the response, that's a completely inaccurate statement. > > > > Here's an example from the Django docs: > > > > > https://docs.djangoproject.com/en/1.3/topics/db/models/#intermediary-manyt > > omany > > > > The Group object has through="Membership". > > > > Although, one thing I notice is that it looks like in the Django example > > it is only using 1 ManyToMany field (e.g. the Person model does not have > a > > manytomany field). Is that required? Only one of the related items can > > have a ManyToMany declared? > > > > That wouldn't make a lot of sense though, right? I would want to ask for > > all Restaurants a Hood asand vice versa. I'd want all of the Hoods > > that belong to a Restaurant. > > > > You only place it on one side of the relationship. Django magic adds > the appropriate methods to the other side of the relationship. DRY - > Dont Repeat Yourself. > > Cheers > > Tom > > -- > 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 > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: ManyToMany problem
On Thu, Sep 29, 2011 at 5:20 PM, James DeMichelewrote: > Thanks for the response, that's a completely inaccurate statement. > > Here's an example from the Django docs: > > https://docs.djangoproject.com/en/1.3/topics/db/models/#intermediary-manyt > omany > > The Group object has through="Membership". > > Although, one thing I notice is that it looks like in the Django example > it is only using 1 ManyToMany field (e.g. the Person model does not have a > manytomany field). Is that required? Only one of the related items can > have a ManyToMany declared? > > That wouldn't make a lot of sense though, right? I would want to ask for > all Restaurants a Hood asand vice versa. I'd want all of the Hoods > that belong to a Restaurant. > You only place it on one side of the relationship. Django magic adds the appropriate methods to the other side of the relationship. DRY - Dont Repeat Yourself. Cheers Tom -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
RE: ManyToMany problem
Thanks for the response, that's a completely inaccurate statement. Here's an example from the Django docs: https://docs.djangoproject.com/en/1.3/topics/db/models/#intermediary-manyt omany The Group object has through="Membership". Although, one thing I notice is that it looks like in the Django example it is only using 1 ManyToMany field (e.g. the Person model does not have a manytomany field). Is that required? Only one of the related items can have a ManyToMany declared? That wouldn't make a lot of sense though, right? I would want to ask for all Restaurants a Hood asand vice versa. I'd want all of the Hoods that belong to a Restaurant. Thanks. -Jamie -Original Message- From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Shawn Milochik Sent: Thursday, September 29, 2011 11:07 AM To: django-users@googlegroups.com Subject: Re: ManyToMany problem I don't think you can make the 'through' argument a string, and I don't think there's circular dependency issue with the 'through' kwarg. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: ManyToMany problem
I don't think you can make the 'through' argument a string, and I don't think there's circular dependency issue with the 'through' kwarg. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
ManyToMany problem
Hello, I have created my own ManyToMany table, but am getting an error when I attempt to get a related item from one of my Models. Any insight would be greatly appreciated. I am on Django 1.3.1. *models/restuarant_hood_map.py* class RestaurantHoodMap(models.Model): restaurant = models.ForeignKey(Restaurant) hood = models.ForeignKey(Hood) class Meta: db_table="restaurant_hood_map" app_label="delivery" Error: AttributeError at 'str' object has no attribute '_default_manager' *models/hood.py* class Hood(models.Model): name = models.CharField(unique=True) restaurants=models.ManyToManyField("restaurant.Restaurant", through="restaurant_hood_map.RestaurantHoodMap") class Meta: db_table = "hoods" app_label="delivery" *models/restaurant.py* class Restaurant(models.model): name=models.ForeignKey(CompanyName) is_active=models.BooleanField(default=True) hoods=models.ManyToManyField("hood.Hood", through="restaurant_hood_map.RestaurantHoodMap") class Meta: db_table="restaurants" app_label="delivery" Now, I know what the problem is.when I attempt to get the related restaurants off of Hood, I am getting this error. That's because it's treating the "restaurant.Restaurant" as a string value. However, I thought it was accepted to use string values in order to avoid circular dependencies? At least thats what I thought from the docs: https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey However, that doesn't seem to work when you get a Hood object and do hood.restaurantsit barfs. Thanks for the help! -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: ManyToMany problem
Thank you, it solved this problem :) Now, I've encountered another issue, which you can see below. When using only single item in annotation it works, but when using two, it returns the same result. I've added default ordering in all of my models, and as you can se, I've tried including it directly, with no success. >>> Event.objects.all().annotate(c=Count('comments'))[0].c 1 >>> Event.objects.all().annotate(p=Count('participants'))[0].p 2 >>> Event.objects.all().annotate(c=Count('comments'), >>> p=Count('participants'))[0].c 2 >>> Event.objects.all().annotate(c=Count('comments'), >>> p=Count('participants'))[0].p 2 >>> Event.objects.all().annotate(c=Count('comments'), >>> p=Count('participants')).order_by('-c', 'p')[0].p 2 >>> Event.objects.all().annotate(c=Count('comments'), >>> p=Count('participants')).order_by('-c', 'p')[0].c 2 Thank you for you help. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: ManyToMany problem
On Thu, Feb 17, 2011 at 5:01 AM, Amarwrote: > OK, I've created a barebone app which shows this problem, so if anyone > can try this and duplicate, I'll report this as a bug (unless there is > a problem wih my code) > You can get it here http://rapidshare.com/files/448393624/problem.zip > > I'm using PostgreSQL database, so change accordingly (unless it only > occurs when using PostgreSQL, which I doubt). > In fact I suspect the behavior is likely observed only on PostgreSQL, due to your queryset having no ordering defined. In that case PostgreSQL often will return the same element for different OFFSET values (any DB could do this, but PostgreSQL is the only one where I've seen it happening regularly). This ticket describes the problem: http://code.djangoproject.com/ticket/9006 Fix is to force force an ordering on the queryset or turn it into a list before indexing into it. Karen -- http://tracey.org/kmt/ -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: ManyToMany problem
OK, I've created a barebone app which shows this problem, so if anyone can try this and duplicate, I'll report this as a bug (unless there is a problem wih my code) You can get it here http://rapidshare.com/files/448393624/problem.zip I'm using PostgreSQL database, so change accordingly (unless it only occurs when using PostgreSQL, which I doubt). Also, you might have to change '#!/usr/bin/env python2' in manage.py, because in my distribution, 'python' defaults to python3. I've also attached console output for testing this, and it will also show this problem when you access root of the webpage (input some data before accessing it, because I assumed there is some data in the database when showing the view) -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
ManyToMany problem
Hi, I have a problem with accessing items using ManyToMany relationship. Here is a sample from shell: >>> parts = Event.objects.all()[0].participants.all() >>> parts [, ] >>> parts[0] >>> parts[1] >>> for p in parts: ... print p ... P1 P2 I cant figure out why accessing parts[0] and parts[1] gives the same result, but loop works. Code: class Event(models.Model): ... participants = models.ManyToManyField(Participant, through='EventParticipant') class EventParticipant(models.Model): ... participant = models.ForeignKey(Participant) event = models.ForeignKey(Event, related_name="event_participants") There is also a Participant class with nothing special inside. All class have __unicode__ method to return name of the event/participant. I'm still learning django, so any help will be apreciated :D -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: ManyToMany problem
On Wed, May 27, 2009 at 4:18 PM, MartinBorthirywrote: > > > > On May 27, 6:11 pm, Alex Gaynor wrote: > > On Wed, May 27, 2009 at 4:07 PM, MartinBorthiry > > wrote: > > > > > > > > > > > > > Hello, > > > > > I have this model with a manyToMany relationship: > > > > > class Group(BaseClass): > > > ... > > >members = models.ManyToManyField > > > (Client ,through='Membership',related_name="members" > > > , blank=True) > > >. > > > class Membership(BaseClass): > > >client = models.ForeignKey(Client) > > >group = models.ForeignKey(Group) > > > > > When i trying to add a new member with this code: > > > > >client = Client.objects.get(pk=client_id) > > >group = Group.objects.get(pk=group_id) > > >group.members.add(client) > > > > > This error message appear: > > > > > 'ManyRelatedManager' object has no > > > attribute 'add'" > > > Where is my error? > > > > When you use an explicit through model you can't use the add() helper, > since > > it doesn't know what data to put in the fields on the other model, you > need > > to construct the intermediary model by hand and set the fkeys to the > right > > values. > > > > Alex > > > > -- > > "I disapprove of what you say, but I will defend to the death your right > to > > say it." --Voltaire > > "The people's good is the highest law."--Cicero > > > Thanks! > understood , so i have to do it this way > > r = Membership(client = client, group= group) >r.save() > > > Yes, usually the reason for an intermediary model is to add fields, if there were more fields django wouldn't know what values to insert. There's a ticket somewhere in trac to add a feature such that you could still use add() if there were no new fields, or if all the fields had a default. Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: ManyToMany problem
On May 27, 6:11 pm, Alex Gaynorwrote: > On Wed, May 27, 2009 at 4:07 PM, MartinBorthiry > wrote: > > > > > > > Hello, > > > I have this model with a manyToMany relationship: > > > class Group(BaseClass): > > ... > > members = models.ManyToManyField > > (Client ,through='Membership',related_name="members" > > , blank=True) > > . > > class Membership(BaseClass): > > client = models.ForeignKey(Client) > > group = models.ForeignKey(Group) > > > When i trying to add a new member with this code: > > > client = Client.objects.get(pk=client_id) > > group = Group.objects.get(pk=group_id) > > group.members.add(client) > > > This error message appear: > > > 'ManyRelatedManager' object has no > > attribute 'add'" > > Where is my error? > > When you use an explicit through model you can't use the add() helper, since > it doesn't know what data to put in the fields on the other model, you need > to construct the intermediary model by hand and set the fkeys to the right > values. > > Alex > > -- > "I disapprove of what you say, but I will defend to the death your right to > say it." --Voltaire > "The people's good is the highest law."--Cicero Thanks! understood , so i have to do it this way r = Membership(client = client, group= group) r.save() --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: ManyToMany problem
On Wed, May 27, 2009 at 4:07 PM, MartinBorthirywrote: > > Hello, > > I have this model with a manyToMany relationship: > > class Group(BaseClass): > ... >members = models.ManyToManyField > (Client ,through='Membership',related_name="members" > , blank=True) >. > class Membership(BaseClass): >client = models.ForeignKey(Client) >group = models.ForeignKey(Group) > > > When i trying to add a new member with this code: > >client = Client.objects.get(pk=client_id) >group = Group.objects.get(pk=group_id) >group.members.add(client) > > This error message appear: > > 'ManyRelatedManager' object has no > attribute 'add'" > Where is my error? > > > When you use an explicit through model you can't use the add() helper, since it doesn't know what data to put in the fields on the other model, you need to construct the intermediary model by hand and set the fkeys to the right values. Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
ManyToMany problem
Hello, I have this model with a manyToMany relationship: class Group(BaseClass): ... members = models.ManyToManyField (Client ,through='Membership',related_name="members" , blank=True) . class Membership(BaseClass): client = models.ForeignKey(Client) group = models.ForeignKey(Group) When i trying to add a new member with this code: client = Client.objects.get(pk=client_id) group = Group.objects.get(pk=group_id) group.members.add(client) This error message appear: 'ManyRelatedManager' object has no attribute 'add'" Where is my error? --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: ManyToMany problem
Thanks, Meir. On Jan 7, 11:24 am, mksoftwrote: > Hi Alex, > > On Jan 4, 12:27 pm, knight wrote: > > > > > Hi, > > > I have the following problem: > > I have models.py with the following class: > > > class Section(Feed): > > parent = models.ManyToManyField('self', blank=True, null=True) > > depth = models.IntegerField(editable=False) > > def save(self): > > self.depth = 1 > > if self.parent: > > self.depth = self.parent.depth + 1 > > return super(Section, self).save() > > def __unicode__(self): > > return "Section_%s" %(self.title) > > Looks like the problem is trying to access the m2m property before the > instance is saved > for the 1st time - no id means ManyToMany relations will fail. In > `save` you're > trying to access `parent` (should be called `parents` IMO, plural) > which is m2m > and trigger the error. > > * You'll need to check if you have an id, if not, save it once to get > it. After that, calculate and > save again (blah). > > * You can't access `parent` like that, m2m behaves like a list, so > you'll have to check members > of that list - which is not clear how is going to be achieved. If > different parents have different levels > which one will you use ? > > * Another problem: If parent's level is changed, the children won't be > updated, potential mess > > * Last thing: Treading this path might lead to recursion (item with > parents which have the > item in their parent, or up the tree, as well), you might wanna > rethink this setup. > > > > > and admin.py with the following classes: > > > class SectionInline(admin.TabularInline): > > model = Section > > extra = 2 > > verbose_name_plural = "Sub Sections" > > > class SectionAdmin(admin.ModelAdmin): > > inlines = [ > > SectionInline, ItemInline > > ] > > > When I try to save new Section from the admin page, I get the > > following error: > > > 'Section' instance needs to have a primary key value before a many-to- > > many relationship can be used. > > > There are 2 things that I don't understand: > > > 1.) Why am I getting this error, if my ManyToMany relation is optional > > 2.) I tried many things including defining id of the Section with > > AutoField but still without luck. > > > Does anyone have an idea what is my problem/mistake here? > > > Thanks, Arshavski Alexander. > > Cheers > -- > Meir Kriheli --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: ManyToMany problem
Hi Alex, On Jan 4, 12:27 pm, knightwrote: > Hi, > > I have the following problem: > I have models.py with the following class: > > class Section(Feed): > parent = models.ManyToManyField('self', blank=True, null=True) > depth = models.IntegerField(editable=False) > def save(self): > self.depth = 1 > if self.parent: > self.depth = self.parent.depth + 1 > return super(Section, self).save() > def __unicode__(self): > return "Section_%s" %(self.title) > Looks like the problem is trying to access the m2m property before the instance is saved for the 1st time - no id means ManyToMany relations will fail. In `save` you're trying to access `parent` (should be called `parents` IMO, plural) which is m2m and trigger the error. * You'll need to check if you have an id, if not, save it once to get it. After that, calculate and save again (blah). * You can't access `parent` like that, m2m behaves like a list, so you'll have to check members of that list - which is not clear how is going to be achieved. If different parents have different levels which one will you use ? * Another problem: If parent's level is changed, the children won't be updated, potential mess * Last thing: Treading this path might lead to recursion (item with parents which have the item in their parent, or up the tree, as well), you might wanna rethink this setup. > and admin.py with the following classes: > > class SectionInline(admin.TabularInline): > model = Section > extra = 2 > verbose_name_plural = "Sub Sections" > > class SectionAdmin(admin.ModelAdmin): > inlines = [ > SectionInline, ItemInline > ] > > When I try to save new Section from the admin page, I get the > following error: > > 'Section' instance needs to have a primary key value before a many-to- > many relationship can be used. > > There are 2 things that I don't understand: > > 1.) Why am I getting this error, if my ManyToMany relation is optional > 2.) I tried many things including defining id of the Section with > AutoField but still without luck. > > Does anyone have an idea what is my problem/mistake here? > > Thanks, Arshavski Alexander. Cheers -- Meir Kriheli --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
ManyToMany problem
Hi, I have the following problem: I have models.py with the following class: class Section(Feed): parent = models.ManyToManyField('self', blank=True, null=True) depth = models.IntegerField(editable=False) def save(self): self.depth = 1 if self.parent: self.depth = self.parent.depth + 1 return super(Section, self).save() def __unicode__(self): return "Section_%s" %(self.title) and admin.py with the following classes: class SectionInline(admin.TabularInline): model = Section extra = 2 verbose_name_plural = "Sub Sections" class SectionAdmin(admin.ModelAdmin): inlines = [ SectionInline, ItemInline ] When I try to save new Section from the admin page, I get the following error: 'Section' instance needs to have a primary key value before a many-to- many relationship can be used. There are 2 things that I don't understand: 1.) Why am I getting this error, if my ManyToMany relation is optional 2.) I tried many things including defining id of the Section with AutoField but still without luck. Does anyone have an idea what is my problem/mistake here? Thanks, Arshavski Alexander. --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---