I' ve the following simplified model structure:
#common/models.py
class CLDate(models.Model):
active = models.BooleanField(default=True)
last_modified = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
#br/models.py
class Dokument(CLDate):
user = models.ForeignKey(User)
class Entity(CLDate):
dokument = models.ForeignKey(Dokument)
. Both class inherits from CLDate, and i' ve a OneToMany relation between them. When i try to migrate, i got the following error:
python manage.py makemigrations
SystemCheckError: System check identified some issues:
ERRORS:
br.Entity.dokument: (models.E006) The field 'dokument' clashes with the
field 'dokument' from model 'common.cldate'.
I can' t really get why is this structure a problem for Django hence the Entity is a totally different object than the Dokument. Could anyone explain me why, and how could i solve it with this structure? So both should inherit from CLDate and there should be this kind of relation between the 2 models from the br application.
I also tried to delete all the migration files, and solve it that way, but the same. Runserver gives also this error.
Django: 1.11.2
Python: 3.4.2
Debian: 8.8
.
Thanks.
If i rename the dokument property name in the Entity model, it works fine.
I' m also almost pretty the same layout was working previously (in previous Django versions).
Since you are using multi-table inheritance, Django creates an implicit one-to-one field from Dokument to CLDate. The reverse relation dokument from CLDate to Dokument is clashing with your Entity.dokument field.
If you don't want to rename your Entity.dokument field, then your other option is to explicitly define the parent link field from Dokument to CLDate and set related_name.
class Dokument(CLDate):
cl_date = models.OneToOneField(CLDate, parent_link=True, related_name='related_dokument')
user = models.ForeignKey(User)
Related
I'm using Django 1.8, Python 2.7.15. Junior dev.
I created a model Test with many to many field to Django Group model:
class Test(models.Model):
group = models.ManyToManyField(Group)
I've made a migration, my project was working, all was good.
Now I had to create Group's proxy model with specyfic managers:
class ExtendedGroup(Group):
get_all = AllGroupsManager()
objects = DefaultGroupManager()
class Meta:
proxy = True
And I had to change my Test model group field (is has methods that must use new managers... so I have to use ExtendedGroup):
I'm changing ManyToManyField relation from Group to ExtendedGroup.
class Test(models.Model):
group = models.ManyToManyField(ExtendedGroup)
ExtendedGroup is proxy model so I think Test's group field should be related to Group even if I use ExtendedGroup (becouse it is proxy model).
But when I run server I got ProgrammingError:
column test_test_group.extendedgroup_id does not exist
When I "makemigrations" I can see that Django want to Alter group field with new relation to model (ExtendedGroup) that actually does not have any tables in database becouse it is proxy model.
operations = [
migrations.AlterField(
model_name='test',
name='group',
field=models.ManyToManyField(to='mainpage.ExtendedGroup'),
),
]
What is going here? Is it right? Why Django want to create relation to model that does not have any tables?
I just don't understand it.
I will appreciate any help!
EDIT:
When I run migration (migrate) I get a bunch of errors:
AttributeError: 'ManyToManyField' object has no attribute
'm2m_reverse_field_name'
So it does not work :(
Create a Django project with an app (add it to INSTALLED_APPS!) with the following models.py:
from django.db import models
class BaseTransaction(models.Model):
pass
class SubscriptionTransaction(BaseTransaction):
class Meta:
index_together = ["id", "canceled"]
canceled = models.BooleanField()
Then the things work this way:
$ python3 manage.py makemigrations
SystemCheckError: System check identified some issues:
ERRORS:
testprj.SubscriptionTransaction: (models.E016) 'index_together' refers to field 'id' which is not local to model 'SubscriptionTransaction'.
HINT: This issue may be caused by multi-table inheritance.
Please explain the reason of this error (there is no multi-table inheritance here) and how to make my code to work.
The problem happens with Django 1.10.3 with both Python 3.5.3 and Python 2.7.13. Is it a Django bug? What's about a workaround?
You are getting this error because the id you are using is in other table and this could be generate a lot of problems.
But if you don't need BaseTransaction table you can mark it as abstract, then you can use your index_together perfectly.
class BaseTransaction(models.Model):
class Meta:
abstract = True
I have a circular dependency in my Django models, such that model A has a foreign key reference to B, while B has a many-to-many reference to A. I've consulted other SO posts and have used the string model names instead of the actual classes, but to no avail. Here are abbreviated versions of my two classes:
User model
import listings.models
class User(models.Model):
...
favorites = models.ManyToManyField('listings.models.Listing')
Listing model
import users.models
class Listing(models.Model):
...
owner = models.ForeignKey('users.models.User')
Every time I attempt to run syncdb, it outputs the following error:
Error: One or more models did not validate: users.user: 'favorites'
has an m2m relation with model listings.models.Listing, which has
either not been installed or is abstract. listings.listing: 'owner'
has a relation with model users.models.User, which has either not
been installed or is abstract.
How do I resolve this without sacrificing the established relationship?
'listings.models.Listing' should be 'listings.Listing'
'users.models.User' should be 'users.User' (or 'auth.User' if you were to use django.contrib.auth.models.User)
Refer to official documentation for more.
You can just delete your imports because, you're not depend on them on code. You use just string with model name - it's not a dependency.
Also you should delete models - from your strings because you can refer to your model as app_name.model_name
I have a model such as the following:
class Item(models.Model):
name = models.CharField(max_length=150)
created = models.DateTimeField(auto_now_add=True)
the admin class is the following:
class ItemAdmin(admin.ModelAdmin):
list_display = ('name', 'created')
the created field does not seem to exist
Is there some basic Django knowledge that I am missing or have forgotten?
When you say the field does not exist, do you mean that it is not showing on the admin change form? This is expected behaviour when using auto_now_add. If you want the field to get a default value on creation but still be editable, use default=datetime.datetime.now instead.
Strange. I tried out your example and it worked perfectly well (Django 1.2.1, Python 2.6.2)
Can you verify that:
The field exists in the database (fire a SQL query perhaps)
Check your admin.py (again) for any differences.
Update
#Daniel's answer is more likely to help the OP.
Just make sure not to forget registering the ItemAdmin in admin.py:
admin.site.register(Item, ItemAdmin)
However, the 'created' field would only be displayed in the Item's list page, as well as if you add an additional field such as:
updated = models.DateTimeField(auto_now=True)
I'm working on what I think is a pretty standard django site, but am having trouble getting my admin section to display the proper fields.
Here's my models.py:
class Tech(models.Model):
name = models.CharField(max_length = 30)
class Project(models.Model):
title = models.CharField(max_length = 50)
techs = models.ManyToManyField(Tech)
In other words, a Project can have different Tech objects and different tech objects can belong to different Projects (Project X was created with Python and Django, Project Y was C# and SQL Server)
However, the admin site doesn't display any UI for the Tech objects. Here's my admin.py:
class TechInline(admin.TabularInline):
model = Tech
extra = 5
class ProjectAdmin(admin.ModelAdmin):
fields = ['title']
inlines = []
list_display = ('title')
admin.site.register(Project, ProjectAdmin)
I've tried adding the TechInline class to the inlines list, but that causes a
<class 'home.projects.models.Tech'> has no ForeignKey to <class 'home.projects.models.Project'>
Error. Also tried adding techs to the fields list, but that gives a
no such table: projects_project_techs
Error. I verified, and there is no projects_project_techs table, but there is a projects_tech one. Did something perhaps get screwed up in my syncdb?
I am using Sqlite as my database if that helps.
I've tried adding the TechInline class to the inlines list, but that causes a
'TechInLine' not defined
Is that a straight copy-paste? It looks like you just made a typo -- try TechInline instead of TechInLine.
If your syncdb didn't create the proper table, you can do it manually. Execute this command:
python manage.py sqlreset <myapp>
And look for the definition for the projects_project_techs table. Copy and paste it into the client for your database.
Assuming your app is called "projects", the default name for your techs table will be projects_tech and the projects table will be projects_project.
The many-to-many table should be something like projects_project_techs
#John Millikin - Thanks for the sqlreset tip, that put me on the right path. The sqlreset generated code that showed me that the projects_project_techs was never actually created. I ended up just deleting my deb.db database and regenerating it. techs then showed up as it should.
And just as a sidenote, I had to do an admin.site.register(Tech) to be able to create new instances of the class from the Project page too.
I'll probably post another question to see if there is a better way to implement model changes (since I'm pretty sure that is what caused my problem) without wiping the database.