If I understand your case correctly, NamedEntity and Entity contain some common fields/methods, that should be present in all children. So you should make them abstract and use them like this:
from django.db import models
class Entity(models.Model):
common_field = models.CharField(max_length=100) # some common field for all children
class Meta:
abstract = True
class CustomEntity(Entity):
custom_field = models.CharField(max_length=100) # only present in CostomEntities
# CustomEntity also has common_field in this example, so you could do that:
ce = CustomEntity(common_field='foo', custom_field='bar')
ce.save()
# But you can't do that with abstract models:
Entity(common_field='baz').save() # will raise an exception
So in your case there're three abstract models: Entity -> NamedEntity -> SpeedyComposerNamedEntity -> (non-abstract models go here)
All abstract models have to have "abstract = True" attribute in their Meta classes.
Here's a real life example from my project (I needed created/updated fields for some of my models):
from django.db.models import Mode, DateTimeField
class TrackedModel(Model):
created = DateTimeField(auto_now_add=timezone.now)
modified = DateTimeField(auto_now=True)
class Meta:
abstract = True
class Event(TrackedModel):
description = ...
...
To reiterate, abstract models can't be saved like usual ones. So you can't use Entity for base class and also to store some data in DB. Probably need another level ob abstraction here.
On 13 Dec 2016, at 19:18, Uri Even-Chen <uri@speedy.net> wrote:If I set SpeedyComposerNamedEntity to be abstract (which makes sense, there will not be objects of it), I get this error message:peedy\composer>manage.py makemigrationsSystemCheckError: System check identified some issues:ERRORS:compose.Composition.accompaniment: (models.E006) The field 'accompaniment' clashes with the field 'accompaniment' from model 'accounts.namedentity'.compose.Composition.folder: (models.E006) The field 'folder' clashes with the field 'folder' from model 'accounts.namedentity'.compose.Composition.user: (models.E006) The field 'user' clashes with the field 'user' from model 'accounts.entity'.compose.Folder.user: (models.E006) The field 'user' clashes with the field 'user' from model 'accounts.entity'.And if I set NamedEntity to be abstract, I get this error message:speedy\composer>manage.py makemigrationsSystemCheckError: System check identified some issues:ERRORS:compose.Composition.accompaniment: (models.E006) The field 'accompaniment' clashes with the field 'accompaniment' from model 'accounts.entity'.compose.Composition.folder: (models.E006) The field 'folder' clashes with the field 'folder' from model 'accounts.entity'.compose.Composition.user: (models.E006) The field 'user' clashes with the field 'user' from model 'accounts.entity'.compose.Folder.user: (models.E006) The field 'user' clashes with the field 'user' from model 'accounts.entity'.Entity can't be abstract because it contains all the entities and usernames on the database, it must have a table.Thanks,Uri.
Uri Even-Chen
Phone: +972-54-3995700
Email: uri@speedy.net
Website: http://www.speedysoftware.com/uri/en/On Tue, Dec 13, 2016 at 5:56 PM, GMail <roboslone@gmail.com> wrote:Hi!First guess - SpeedyComposerNamedEntity is not an abstract model. Meta is not inherited from NamedEntity, you have to do that explicitly:classSpeedyComposerNamedEntity( NamedEntity): ...class Meta(NamedEntity.Meta):pass...On 13 Dec 2016, at 18:30, Uri Even-Chen <uri@speedy.net> wrote:--Dear Django Users,I'm trying to write the models of Speedy Composer in Django 1.10.4, but I can't run makemigrations - I get this error message:speedy\composer>manage.py makemigrationsSystemCheckError: System check identified some issues:ERRORS:compose.Composition.accompaniment: (models.E006) The field 'accompaniment' clashes with the field 'accompaniment' from model 'compose. speedycomposernamedentity'. compose.Composition.folder: (models.E006) The field 'folder' clashes with the field 'folder' from model 'compose.speedycomposernamedentity'. compose.Composition.user: (models.E006) The field 'user' clashes with the field 'user' from model 'accounts.entity'.compose.Folder.user: (models.E006) The field 'user' clashes with the field 'user' from model 'accounts.entity'.This is my new models.py file of my new app, speedy.composer.compose (from PyCharm): from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from speedy.net.accounts.models import NamedEntity
class SpeedyComposerNamedEntity(NamedEntity): 1
MIN_USERNAME_LENGTH =
MAX_USERNAME_LENGTH = 200
MIN_SLUG_LENGTH = 1
MAX_SLUG_LENGTH = 200
MIN_NAME_LENGTH = 1
MAX_NAME_LENGTH = 200
def __str__(self):
return '{}'.format(self.name)
class ChordsTemplate(SpeedyComposerNamedEntity): class Meta:
verbose_name = _('chords template')
verbose_name_plural = _('chords templates')
class Accompaniment(SpeedyComposerNamedEntity): class Meta:
verbose_name = _('accompaniment')
verbose_name_plural = _('accompaniments')
class Folder(SpeedyComposerNamedEntity): verbose_name
user = models.ForeignKey(=_( 'user'), to=settings.AUTH_USER_MODEL, related_name='+')
class Meta:
verbose_name = _('folder')
verbose_name_plural = _('folders')
class Composition(SpeedyComposerNamedEntity): verbose_name
user = models.ForeignKey(=_( 'user'), to=settings.AUTH_USER_MODEL, related_name='+')
folder = models.ForeignKey(verbose_name=_( 'folder'), to=Folder, related_name='+')
chords_template = models.ForeignKey(verbose_name=_( 'chords template'), to=ChordsTemplate, related_name='+')
accompaniment = models.ForeignKey(verbose_name=_( 'accompaniment'), to=Accompaniment, related_name='+')
tempo = models.SmallIntegerField(verbose_name =_('tempo'), default=105)
public = models.BooleanField(verbose_name =_('public'), default=False)
class Meta:
verbose_name = _('composition')
verbose_name_plural = _('compositions')
You can see the speedy.net code on GitHub: https://github.com/urievenchen/speedy-net
What did I do wrong? Did I write the models wrongly?Thanks,Uri.
Uri Even-Chen
Phone: +972-54-3995700
Email: uri@speedy.net
Website: http://www.speedysoftware.com/uri/en/
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com .
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users .
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ .CAMQ2MsGdLb%2BHXtGF4ss_vDb_ fsW%3DHv4e5e% 3DUskbBJfzt3CFunw%40mail. gmail.com
For more options, visit https://groups.google.com/d/optout .--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com .
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users .
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6DD8F859- .10C2-4670-9446-49F59E7D1165% 40gmail.com
For more options, visit https://groups.google.com/d/optout .--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMQ2MsGG8c0sc7ivU-GjcL3JGDTNPF%2BQGSHGx-c9fGUHnw1jXA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


No comments:
Post a Comment