Django inherited models foreign key clash - python

I am making a Q&A site like stackoverflow. But Django model field clashes.
I am using Django2.1.5
I tried to add related_name = 'answers_case' but nothing changed.
Here is my models:
class Post(models.Model):
user = models.ForeignKey(MyUser, on_delete=models.CASCADE)
post_date = models.DateTimeField(auto_now_add=True)
score = models.IntegerField(default=0)
body = models.TextField()
last_edit_date = models.DateTimeField(null=True, blank=True)
is_active = models.BooleanField(default=False)
class Case(Post):
title = models.CharField(max_length=150)
slug = models.SlugField(unique=True, max_length=150)
view_count = models.IntegerField(default=0)
keyword = models.ManyToManyField(Keyword)
class Answer(Post):
case = models.ForeignKey(Case, on_delete=models.CASCADE, related_name='answers_case')
is_accepted = models.BooleanField(default=False)
And the error is:
Unhandled exception in thread started by .wrapper at 0x7fa57c42c7b8>
Traceback (most recent call last):
File "/home/emre/anaconda3/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/home/emre/anaconda3/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/home/emre/anaconda3/lib/python3.6/site-packages/django/core/management/base.py", line 425, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
medicus_website.Answer.case: (models.E006) The field 'case' clashes with the field 'case' from model 'medicus_website.post'.
System check identified 1 issue (0 silenced).

You fell into a trap of models inheritance in django. You are applying the Multi-table inheritance, while I assume you are expecting the Abstract inheritance.
In other words, your Post already has a case field - since you have multi-table inheritance from Case to Post, each Post might have corresponding Case, and that's why you have the name clash. If you want to maintain current behaviour (in terms of db structure and relations construction) you will have to rename either Case model or case field in answer. On the other hand, if you'd like to switch to abstract inheritance, with each table self-conatined, you could do this like this:
class AbstractPost(models.Model):
user = models.ForeignKey(MyUser, on_delete=models.CASCADE)
post_date = models.DateTimeField(auto_now_add=True)
score = models.IntegerField(default=0)
body = models.TextField()
last_edit_date = models.DateTimeField(null=True, blank=True)
is_active = models.BooleanField(default=False)
class Meta:
abstract = True
class Post(AbstractPost):
# if you need standalone posts
pass
class Case(AbstractPost):
title = models.CharField(max_length=150)
slug = models.SlugField(unique=True, max_length=150)
view_count = models.IntegerField(default=0)
keyword = models.ManyToManyField(Keyword)
class Answer(Post):
case = models.ForeignKey(Case, on_delete=models.CASCADE, related_name='answers_case')
is_accepted = models.BooleanField(default=False)

Related

I cant use the objects attribute in django project

I created a Projects model
class Projects(models.Model):
name = models.CharField(max_length=257, unique=True, null=False)
description = models.TextField()
active_issue_count = models.IntegerField(default=0) # functiona bağlanmalı
solved_issue_count = models.IntegerField(default=0) # functiona bağlanmalı
is_active = models.BooleanField()
start_date = models.DateTimeField()
deadline = models.DateTimeField()
and I want to use this project model Projects.objects.all() with this code but when I typed in pyCharm shows me this suggestion
but while I try to use this model
class User(AbstractUser, PermissionsMixin):
objects = UserManager()
related_group = models.CharField
current_project = models.ForeignKey(to='core.Project',
related_name='current_project', on_delete=models.PROTECT, null=True)
total_worked_project = models.IntegerField(default=0) # functiona bağla
active_work_project_count = models.IntegerField(default=0) # functiona bağla
REQUIRED_FIELDS = ['email']
`
I can use User.objects.all() without suggestion what should I do anyone can help me ?
PyCharm just don't follow some object's relation. Here it has no clue that this Model class has related Manager, that you can call with objects.
You can ignore it in PyCharm so it will not bother you.
More actions... => Ignore (...)

How can i perform the right reverse query in ManytoMany in Django

I'm trying to perform a reversed query for a manytomany fields in Django, but it keeps gives me nothing, here is my code
models.py
class Product(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
name = models.CharField(max_length=120)
image = models.ImageField(upload_to='products')
branch = models.ManyToManyField(Branch, related_name='branches')
class Branch(models.Model):
area = models.ForeignKey(Area, on_delete=CASCADE)
name = models.CharField(max_length=1200)
phone = models.CharField(max_length=30, null=True, blank=True)
address = models.CharField(max_length=1200, null=True, blank=True)
tax_value = models.DecimalField(decimal_places=2, max_digits=4)
views.py
for branch in product_object.branches.all():
print(branch)
The branch is always nothing !!
For some reason, the related name is not calling it anymore. I called it using the model name (lower cased).
This is how it worked
for branch in product_object.branch.all():
Just to complete your answer above, I think the way you have your model set up is a little misleading and confusing. I think this would improve clarity:
class Product(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
name = models.CharField(max_length=120)
image = models.ImageField(upload_to='products')
branches = models.ManyToManyField(Branch, related_name='products')
Since you have a many to many field, a product can have multiple branches, the attribute name should reflect that
When you use the related_name, this would be if you are going from the m2m object. For example, if you have a branch, you could get all it's products by doing branch.products

Django UniqueConstraint

Context
I have the models AppVersion, App & DeployApp. In the AppVersion model users can upload APK files to the filesystem. I am using a pre_save signal to prevent uploading APK files with the same version_code for a specific App like this:
#receiver(pre_save, sender=AppVersion)
def prevent_duplicate_version_code(sender, instance, **kwargs):
qs = AppVersion.objects.filter(app_uuid=instance.app_uuid, version_code=instance.version_code)
if qs.exists():
raise FileExistsError("Version code has to be unique for a specific app")
This signal does what I want, except it also raises the error when I am trying to create an object in the bridge-table DeployApp.
Models
# models.py
class App(models.Model):
app_uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, db_index=True)
app_name = models.CharField(max_length=100)
class AppVersion(models.Model):
app_version_uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, db_index=True)
app_uuid = models.ForeignKey(App, on_delete=models.CASCADE, related_name='app_versions')
app_version_name = models.CharField(max_length=100)
version_code = models.IntegerField(blank=True, null=True, editable=False)
source = models.FileField(upload_to=get_app_path, storage=AppVersionSystemStorage())
class DeployApp(models.Model):
deploy_app_uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, db_index=True)
app_version = models.ForeignKey(AppVersion, on_delete=models.CASCADE)
device_group = models.ForeignKey(DeviceGroup, on_delete=models.CASCADE)
release_date = UnixDateTimeField()
My guess is that when creating an object of DeployApp the related AppVersion is also saved and thus the pre_save signal is called and raises the Exception.
I also tried to override the save() method for the AppVersion model but the results are the same.
How do I make sure that the Exception only happens upon creating a new AppVersion instance and does not happen when adding or editing a DeployApp instance?
Solved it thanks to Bear Brown his suggestion. I removed the signal and added UniqueConstraint to the AppVersion model like this:
class Meta:
db_table = 'app_version'
constraints = [
models.UniqueConstraint(fields=['app_uuid', 'version_code'], name='unique appversion')
]

Django form: reference fields from foreign key

I'm making a task tracker webapp (the full source code is also available) and I have a database structure where each task has a title, a description, and some number of instances, that can each be marked incomplete/incomplete:
class Task(models.Model):
title = OneLineTextField()
description = models.TextField(blank=True)
class TaskInstance(models.Model):
task = models.ForeignKey(Task, on_delete=models.CASCADE)
is_complete = models.BooleanField()
The task and the instances can be shared separately, although access to the instance should imply read access to the task. This is intended for classroom situations, where the teacher creates a task and assigns it to their students.
class TaskPermission(models.Model):
task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='permissions')
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='task_permissions_granted')
shared_by = models.ForeignKey(User, on_delete=models.PROTECT, null=True, related_name='task_permissions_granting')
can_edit = models.BooleanField(default=False)
class Meta:
unique_together = 'task', 'user', 'shared_by',
class TaskInstancePermission(models.Model):
task_instance = models.ForeignKey(TaskInstance, on_delete=models.CASCADE, related_name='permissions')
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='task_instance_permissions_granted')
shared_by = models.ForeignKey(User, on_delete=models.PROTECT, null=True, related_name='task_instance_permissions_granting')
can_edit = models.BooleanField(default=False)
class Meta:
unique_together = 'task_instance', 'user', 'shared_by',
My question is how to create a form for TaskInstances with fields for its is_complete, as well as its Task's title and description. Would something like this work? Or would I need to implement my own save and clean methods?
class TaskForm(ModelForm):
class Meta:
model = TaskInstance
fields = ('is_complete', 'task__title', 'task__description')
I think inlineformset_factory is what I'm looking for!
Actually, it does not seem to be useful: it is for multiple forms of the same type, not different types...

'NoneType' object has no attribute 'unique' BUT I'm not using the 'unique' attribute

I'm build models in Django 1.8, and I'm using abstract inheritance (which I'm assuming is contributing to the problem). I have abstract models and then I have models which are based on those abstract models. I also have ForeignKey and ManyToMany relations between some models.
Everything looks fine, but when I try to syncdb or 'makemigrations blog' I get an AttributeError which says 'NoneType' object has no attribute 'unique'.
I don't know why I'm getting it, and I tried experimenting with different model setups, and I read lots of forum posts, but for now I've hit a wall.
I'll post the traceback and my models below:
MODELS:
indie_db
from django.db import models
class URL(models.Model):
link = models.CharField(max_length=400)
name = models.CharField(max_length=200)
def __str__(self):
return self.name
class Artist(models.Model):
name = models.CharField(max_length=200)
description = models.TextField(null=True, blank=True)
link = models.ForeignKey(URL)
class Meta:
abstract = True
ordering = ['name']
def __str__(self):
return self.name
class ArtistSingle(Artist):
birthdate = models.DateField(null=True, blank=True)
deathdate = models.DateField(null=True, blank=True)
class ArtistGroup(Artist):
members = models.ManyToManyField(ArtistSingle)
established = models.DateField(null=True, blank=True)
disbanded = models.DateField(null=True, blank=True)
class Contributor(models.Model):
contributing_artist = models.ForeignKey(ArtistSingle, null=True, blank=True)
alternate_name = models.CharField(max_length=200)
role = models.CharField(max_length=200)
class ProductionCompany(models.Model):
name = models.CharField(max_length=200)
link = models.ForeignKey(URL)
def __str__(self):
return self.name
class Work(models.Model):
title = models.CharField(max_length=200)
description = models.TextField(null=True, blank=True)
created = models.DateField()
city = models.CharField(max_length=200)
production_company = models.ForeignKey(ProductionCompany, blank=True, null=True)
self_published = models.BooleanField(default=False)
creator = models.ForeignKey(Artist)
link = models.ForeignKey(URL)
styles = models.CharField(max_length=200)
contributors = models.ManyToManyField(Contributor)
class Meta:
abstract = True
ordering = ['-created']
def __str__(self):
return self.title
class MusicalWork(Work):
audio_links = models.ManyToManyField(URL)
class WrittenWork(Work):
excerpt = models.TextField(null=True, blank=True)
class PerformanceWork(Work):
venue = models.CharField(max_length=200)
class VideoWork(Work):
length = models.CharField(max_length=16)
class VisualWork(Work):
images = models.ManyToManyField(URL)
blog:
from django.db import models
# Create your models here.
class Tag(models.Model):
tag_name = models.CharField(max_length=200)
def __str__(self):
return self.tag_name
class Entry(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
slug = models.SlugField(max_length=200)
publish = models.BooleanField(default=True)
created = models.DateTimeField(auto_now=True)
modified = models.DateTimeField(auto_now=True)
tags = models.ManyToManyField(Tag)
def __str__(self):
return self.title
class Meta:
verbose_name = "Blog Entry"
verbose_name_plural = "Blog Entries"
ordering = ["-created"]
TRACEBACK:
[pattmayne#web476 weird_canada]$ python3.4 manage.py makemigrations blog
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/management/base.py", line 440, in execute
self.check()
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/management/base.py", line 478, in check
include_deployment_checks=include_deployment_checks,
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/checks/model_checks.py", line 28, in check_all_models
errors.extend(model.check(**kwargs))
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/db/models/base.py", line 1181, in check
errors.extend(cls._check_fields(**kwargs))
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/db/models/base.py", line 1258, in _check_fields
errors.extend(field.check(**kwargs))
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/db/models/fields/related.py", line 1829, in check
errors = super(ForeignKey, self).check(**kwargs)
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/db/models/fields/related.py", line 1502, in check
errors.extend(self._check_unique_target())
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/db/models/fields/related.py", line 1522, in _check_unique_target
for rel_field in self.foreign_related_fields)
File "/home/pattmayne/webapps/limbs_008/lib/python3.4/Django-1.8.2-py3.4.egg/django/db/models/fields/related.py", line 1522, in <genexpr>
for rel_field in self.foreign_related_fields)
AttributeError: 'NoneType' object has no attribute 'unique'
IF this is caused by my inherited models, what is the exact cause, and how should I change things?
Thanks in advance.
I believe the issue is actually with the Work models.
You have a ForeignKey to URL in the abstract class,
link = models.ForeignKey(URL)
And you also have keys to URL in some of the derived classes, for example MusicalWork:
class MusicalWork(Work):
audio_links = models.ManyToManyField(URL)
So MusicalWork has two links to URL. Which would be fine, except Django attempts to create a reverse relationship for URL to your model, usually called musicalwork_set for this case, but it has two reverse relationships for the same model!
The answer would be to specify a related_name field for any models that derive from it with the same model references.
Therefore:
class MusicalWork(Work):
audio_links = models.ManyToManyField(URL, related_name='musicalwork_audio_set')
But there may be other issues as that error message doesn't exactly point to this condition (trust me, Django has a much nicer error message for this particular mistake).
This is because you have a Foreign key to an Abstract Model.
creator = models.ForeignKey(Artist)
You can change this to ArtistSingle
creator = models.ForeignKey(ArtistSingle)
Ideally it should have thrown a proper error like
Field defines a relation with model 'Artist', which is either not installed, or is abstract.
But the crash is a bug with Django 1.8

Categories

Resources