This is code in models.py
class Package(models.Model):
name=models.CharField(max_length=300)
version=models.CharField(max_length=300,blank=True)
home_page=models.URLField(blank=True)
summary=models.TextField()
description=models.TextField(blank=True)
keywords=models.TextField(blank=True)
categories=models.ManyToManyField(Category,related_name='packages')
class Category(models.Model):
topic=models.ForeignKey(Package, related_name="categories")
When i try to syncdb it says "NameError Category is not defined" i tried placing class category first and package next this time it says "NameError name Package is not defined".
Please help me out of this problem.
EDIT:
Thanks for the help guys, from a very little knowledge of what i know in databases the tutorial here seems wrong http://toys.jacobian.org/presentations/2007/pycon/tutorials/beginning/
He has defined the field categories in Packages to be many-to-many and the field in Category topic to be a foreign key to Package ,but a foreign key is a many-to-one relationship, therefore the implementation is flawed.
I think django is trying to tell it in its way as "Accessor for field 'topic' clashes with m2m field 'Package.categories'." Reverse query name for field 'topic' clashes with m2m field ' "
Is that correct?
"If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself..."
class Package(models.Model):
...
categories=models.ManyToManyField('Category', related_name='packages')
Add quotes while you define category ManyToManyField.
Change to:
categories=models.ManyToManyField('Category',related_name='packages')
Reason: Category is not defined while you add that field, so need to add quotes around it so that django will resolve it later.
Related
In OpenERP v7, I need to get all the fields of res.partner model in the travel.partner model
class travel_partner(osv.osv):
_name = "travel.partner"
_inherit = "res.partner"
When creating records in "travel.partner" model works fine. but when trying to create a partner record or user or company gives following error
AttributeError: 'Field image not found in browse_record(travel.partner, 68)'
How can i solve this? or is there any alternate solution other than re coding all the fields to the new table?
You can try by defining relation between travel.partner and res.partner same as product.template and product.product by adding _inherits not _inherit, it will allow you to access all the parent fields in child model.
At current situation I can not say anything without looking your entire code because logically nothing is wrong in your question, so it's better if you should add all the details or just implement another one.
Because when you create res.partner is must not to go for travel.partner as it's child of res.partner.
I got the answer.
The error was because of the functional field with store condition. In res.partner, there were fields image_medium and image_small using store conditions with key as res.partner. In my new model,I re defined the code with store condition key changed to travel.partner which fixed the issue
I have 5 classes, Customer, Supplier, Order, Design and OutSource.
The Order has 3 relationships, one to one with Customer, many to many with Designand one to one with OutSourse. first I just did add its one to one relationship withCustomer(customer=models.ForeignKey(Customer)), now I wanna add the those 2 relationships withDesignandOutsourse`.
design=models.ManyToManyField(Design)
outSource=models.OneToOneField(OutSource)
when I do that and run the "makemigrations" command I get this error--> "NameError:name "Design" is not defined"
without those 2 lines of code I can migrate without any kinda problem...can't figure out where I'm going wrong.
any help will be appreciated.
models.py
class Order(models.Model):
o_type=models.CharField(max_length=15, verbose_name='Order type')
number=models.IntegerField()
date=models.DateField()
status=models.CharField(max_length=25)
delivery_date=models.DateField()
customer=models.ForeignKey(Customer)
design=models.ManyToManyField(Design)
outSource=models.OneToOneField(OutSource)
class Design(models.Model):
dimension=models.IntegerField()
image=models.ImageField(upload_to='images/%Y/%m/%d')
number_of_colors=models.IntegerField()
sides=models.IntegerField(verbose_name='side(s)')
class OutSource(models.Model):
date=models.DateField()
number=models.IntegerField()
description=models.CharField(max_length=100)
code=models.IntegerField()
supplier=models.ForeignKey(Supplier)
You are declaring the class Design after actually defining the ManyToManyField relationship. It cannot find the class with name Design and hence the NameError.
Declare the class Design before class Order and things should work for you.
I'm trying to have a model with 2 ManyToMany fields without allowing a backwards relation.
So here is the model:
class Camp(models.Model):
#...
free_options = models.ManyToManyField('Option', related_name='+')
paid_options = models.ManyToManyField('Option', related_name='+')
After trying to do
python manage.py syncdb
I'm getting the following error:
Error: One or more models did not validate: camps.camp: Accessor for
m2m field 'free_options' clashes with related m2m field 'Option.+'.
Add a related_name argument to the definition for 'free_options'.
camps.camp: Reverse query name for m2m field 'free_options' clashes
with related m2m field 'Option.+'. Add a related_name argument to the
definition for 'free_o ptions'.
Is it not possible to have 2 fields without backwards relation on the same model? how can i fix this?
Thanks!
I would ask why you're bothered by having the backwards relation, just don't use it if you don't want it. But to answer the question, no there isn't a way to remove it entirely.
According to the Django documentation for the related_name argument of the ManyToManyField:
If you have more than one ManyToManyField pointing to the same model
and want to suppress the backwards relations, set each related_name to
a unique value ending with '+'
Emphasis mine.
So if you want to do that, you should be able to simply:
class Camp(models.Model):
#...
free_options = models.ManyToManyField('Option', related_name='free_options+')
paid_options = models.ManyToManyField('Option', related_name='paid_options+')
# ^^^^^^^^^^^^
to suppress the backwards relation on multiple ManyToManyField's in the same model.
Hope this helps!
I recently got a ForeignKey clash in my Django model. I have the need to have two foreign keys (owner, assigned_to) ultimately pointing to the same model (a user).
From what I understand I need a related_name argument to solve that problem. So I did that:
assigned_to = models.ForeignKey(TaskUser, blank=True, null=True, related_name='user_assignment')
and
owner = models.ForeignKey(TaskUser, related_name="user_ownership"
But I'm still getting an error:
tasks.task: Accessor for field 'owner' clashes with related field 'TaskUser.user
_ownership'. Add a related_name argument to the definition for 'owner'.
tasks.task: Reverse query name for field 'owner' clashes with related field 'TaskUser.user_ownership'. Add a related_name argument to the definition for 'owner'.
Why am I still getting this error?
There is one catch, owner is in a super class (BaseWidget) and assigned_to is in a sub class (Task). Are there issues with using related_name in an inheritance relationship? Do I need to just override the inheritance of owner and redefine related_name in the sub class instead? I'd appreciate any help!
If you have ForeignKey relationships in an abstract base class every class inheriting from it will have this relationship. As a result of this you must not 'hardcode' its related_name, because all sub classes will try to create the same accessor on the realted class (TaskUser in this case).
You should better do something like:
owner = models.ForeignKey(TaskUser, related_name="%(app_label)s_%(class)s_ownership")
See the django docs on this.
If you are using related_name in abstract base class you need to use a '%(app_label)s' and '%(class)s' in it.
Its mentioned in django doc
Be careful with related_name
I have a legacy database with non-django naming conventions. If I have the following (cut down) models:
class Registration(models.Model):
projectId=models.IntegerField(primary_key=True)
class Application(models.Model):
applicationId=models.IntegerField(primary_key=True)
registration=models.ForeignKey(Registration,db_column='projectId')
The ForeignKey instance causes a property to be created on Application called registration_id, but this is neither the correct name for the field (I have a hack to fix this), nor is it able to be used in a QuerySet.
Is there some way of using the id field provided by the ForeignKey on the Application model, rather than having to reference it via Registration?
Ie. I write lots of code like:
Application.objects.get(projectId=1234)
And don't want to have to write it out as:
Application.objects.get(registration__projectId=1234)
or even
Application.objects.get(registration__pk=1234)
I'm slightly surprised that:
Application.objects.get(registration_id=1234)
doesn't work...
Also note, I tried defining the id column as a field as well as the foreignkey which worked for queryset, but inserts complain of trying to insert into the same column twice:
class Application(models.Model):
...
projectId=models.IntegerField()
...
Have you tried this?
Application.objects.get(registration=1234)
I think just doing Application.objects.registration.get(projectId=1234) should do what you want.