I've got two models in a Django application that have exactly the same fields, but different types of information is stored in each.
For example:
class A(models.Model)
field_a = models.CharField(primary_key = True, max_length = 24)
field_b = models.CharField(primary_key = True, max_length = 24)
class B(models.Model)
field_a = models.CharField(primary_key = True, max_length = 24)
field_b = models.CharField(primary_key = True, max_length = 24)
It seems like it would make sense to contain these in an abstract model and have these two classes as sub-classes. I was assuming that I could simply do this, without needing to make DB modifications, but Django isn't able to find the fields of my models any longer.
Could someone offer advice?
If you create a new abstract class this won't interfere with your database. As you can see in documentation https://docs.djangoproject.com/en/dev/topics/db/models/#abstract-base-classes abstract classes are just python classes without database impact.
your code could be looks like this:
class Parent(models.Model)
field_a = models.CharField(primary_key = True, max_length = 24)
field_b = models.CharField(primary_key = True, max_length = 24)
class Meta:
abstract = True
class A(Parent)
pass
class B(Parent)
pass
Related
I am developing an app with Django where I have to model systems.
A system is defined by a number of standard fields (name, owner, etc...) and then a tree of assets (implemented using mptt). A given asset can be part of several systems. A node in the tree of assets can point only to a given asset.
So, I have a model to represent the assets (simplified):
class Asset(models.Model):
name = models.CharField(verbose_name = 'name', max_length = 64, unique = True, blank = False, null = False)
I have a class to represent the system trees (simplified):
class System_Tree(MPTTModel):
parent = TreeForeignKey('self',verbose_name = 'parent', on_delete = models.CASCADE, blank = True, null = True, related_name = 'children')
asset = models.ForeignKey(Asset, verbose_name = 'asset', on_delete = models.CASCADE, blank = False, null = False)
I have a class to represent the system (simplified):
class System(models.Model):
name = models.CharField(verbose_name = 'name', max_length = 64, unique = True, blank = False, null = False)
I would like to show in a template the system with its fields and its specific system tree, but I don't know how to relate a specific instance of a system tree (maybe identified by a root node (parent = null)) with the System model.
Should I define a new field type representing system trees?
Should I store only a reference to the parent node id as a big integer field?
Is there any way then to automate the admin interface to show the right forms for a system (the standard fields and then a tree for the system tree)?
Thanks in advance!!
I'm trying to write up a Factory for a model with a GFK for testing but I can't seem to get it working. I've referred to the common recipes in the docs, but my models don't match up exactly, and I'm also running into an error. Here are my models
class Artwork(models.Model):
...
region = models.ForeignKey("Region", on_delete=models.SET_NULL, null=True, blank=True)
class Region(models.Model):
# Could be either BeaconRegion or SpaceRegion
region_content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
region_object_id = models.PositiveIntegerField()
region = GenericForeignKey("region_content_type", "region_object_id")
class SpaceRegion(models.Model):
label = models.CharField(max_length=255)
regions = GenericRelation(
Region,
content_type_field="region_content_type",
object_id_field="region_object_id",
related_query_name="space_region",
)
class BeaconRegion(models.Model):
label = models.CharField(max_length=255)
regions = GenericRelation(
Region,
content_type_field="region_content_type",
object_id_field="region_object_id",
related_query_name="beacon_region",
)
Essentially, an Artwork can be placed in one of two Regions; a SpaceRegion or BeaconRegion.
I've created the following Factorys for the corresponding models
class RegionFactory(factory.django.DjangoModelFactory):
region_object_id = factory.SelfAttribute("region.id")
region_content_type = factory.LazyAttribute(
lambda o: ContentType.objects.get_for_model(o.region)
)
class Meta:
exclude = ["region"]
abstract = True
class BeaconRegionFactory(RegionFactory):
label = factory.Faker("sentence", nb_words=2)
region = factory.SubFactory(RegionFactory)
class Meta:
model = Region
class SpaceRegionFactory(RegionFactory):
label = factory.Faker("sentence", nb_words=2)
region = factory.SubFactory(RegionFactory)
class Meta:
model = Region
class ArtworkFactory(factory.django.DjangoModelFactory):
...
region = factory.SubFactory(SpaceRegionFactory)
In my test, I try to create an Artwork using ArtworkFactory(), but it errors with
AttributeError: The parameter 'region' is unknown. Evaluated attributes are {}, definitions are <DeclarationSet: {'region_object_id': <SelfAttribute('region.id', default=<class 'factory.declarations._UNSPECIFIED'>)>, 'region_content_type': <factory.declarations.LazyAttribute object at 0x1068cf430>, 'label': <factory.faker.Faker object at 0x1068cf880>}>
What am I doing wrong here?
The issue comes when resolving ArtworkFactory.region.region, i.e SpaceRegionFactory.region.
From your models, it seems that:
Region is a table which points to either SpaceRegion or BeaconRegion
SpaceRegion and BeaconRegion are simple tables, with a helper to retrieve the related Region object.
The first step in those complex relation chains is to write the code without factories:
>>> shire = SpaceRegion(label="Shire")
>>> shire_generic = Region(region=shire)
>>> the_ring = Artwork(region=shire_generic)
This tells us that the Region is always created after the SpaceRegion or BeaconRegion, giving the following factories:
class SpaceRegionFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.SpaceRegion
label = factory.Faker("sentence", n_words=2)
class RegionFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Region
region = factory.SubFactory(SpaceRegionFactory)
class ArtworkFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Artwork
region = factory.SubFactory(RegionFactory)
With this, you should be able to get your code working.
Note how we're simply setting the region field on Region: Django's internals will extract the object content type / content ID automatically.
Additional options
You could tune the RegionFactory to let callers decide whether they want a SpaceRegion or a BeaconRegion:
class RegionFactory(factory.django.DjangoModelFactory):
class Meta:
models = Region
class Params:
space = True # Request a SpaceRegion
region = factory.Maybe(
factory.SelfAttribute("space"),
factory.SubFactory(SpaceRegion),
factory.SubFactory(BeaconRegion),
)
Is there any way to parametrize a class in Python? The parametrized class may look something like this:
class FIELDSerializer:
FIELD = serializers.CharField(source='get_FIELD_display', required=False)
class Meta:
model = None
fields = {FIELD}
Which would need to create the following three classes:
class NameSerializer:
name = serializers.CharField(source='get_name_display', required=False)
class Meta:
model = None
fields = {'name'}
class CategorySerializer:
category = serializers.CharField(source='get_category_display', required=False)
class Meta:
model = None
fields = {'category'}
class StateSerializer:
state = serializers.CharField(source='get_state_display', required=False)
class Meta:
model = None
fields = {'state'}
Is this possible or not?
You can do what you want with a factory function, although it's not completely trivial to get the internal variables (attributes) as you want them:
def factory(FIELDname):
class FIELDSerializer:
class Meta:
model = None
fields = {FIELDname}
settatr(FIELDSerializer, FIELDname, serializers.CharField(source=f'get_{FIELDname}_display', required=False))
return FIELDSerializer
CategorySerializer = factory('category')
StateSerializer = factory('state')
NameSerializer = factory('name')
The setattr allows us to set the name of attribute to the FIELDname string. (Thanks to #Code-Apprentice and #juanpa.arrivillaga for this idea.)
I don't know if there's any easy way to avoid the repetition of the field name and the desired class name when you call the factory without using something like exec (which is perfectly legal but usually leaves programmers with a bad taste in their mouths).
I'm using django_tables2, and have ended up with the following two tables which are almost identical:
class UserMapsetTable(Table):
edit = ButtonColumn('Edit', 'mapsets_users_edit')
mappings = ButtonColumn('Mappings', 'mapsets_users_mappings')
class Meta:
model = UserMappingRuleSet
fields = (
'name',
'notes'
)
attrs = responsive_table_attrs()
class ReadingMapsetTable(Table):
edit = ButtonColumn('Edit', 'mapsets_readings_edit')
mappings = ButtonColumn('Mappings', 'mapsets_readings_mappings')
class Meta:
model = ReadingMappingRuleSet
fields = (
'name',
'notes'
)
attrs = responsive_table_attrs()
How do I remove/reduce the duplication?
If they really are this similar, you could write a factory to dynamically create the Table classes for you:
def table_factory(Model, name):
class Table(tables.Table)
edit = ButtonColumn('Edit', 'mapsets_' + name + '_edit')
mappings = ButtonColumn('Mappings', 'mapsets_' + name + '_mappings')
class Meta:
model = Model
fields = (
'name',
'notes'
)
attrs = responsive_table_attrs()
return Table
UserMapsetTable = table_factory(UserMappingRuleSet, 'users')
ReadingMapsetTable = table_factory(ReadingMapRuleSet, 'readings')
In this example, I would not advise doing this. You probably need changing one of the two tables later which will be a PITA.
Another way would be to have some method on the model's returning the right value for mapset_{}_edit. Then you could just change the implementation of your ButtonColumn to ask the model for the correct value.
I'm trying to build two abstract classes called SurveyQuestionBase and SurveyResponseBase that will serve as templates to quickly define new concrete Models for implementing specific surveys on our website. The issue I am having is in enforcing that the SurveyResponseBase model, when made concrete, should define a ForeignKey to a concrete model of SurveyQuestionBase.
Django does not allow us to define ForeignKeys to abstract classes so I cannot, for instance, do this:
question = models.ForeignKey(SurveyQuestionBase)
Neither can I have it as None or app_label.ModelName for similar reasons.
One hacky fix is to create a new concrete model SurveyQuestionConcrete and make the ForeignKey point to this: question = models.ForeignKey(concrete_model), combined with validation to ensure this model is replaced.
Is there a cleaner way to achieve the same thing? All I need to do is ensure that when someone defines a concrete model from SurveyResponseBase they include a ForeignKey to a concrete model defined from SurveyQuestionBase
Here's the full code:
from __future__ import unicode_literals
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
# Implementation borrows from: https://github.com/jessykate/django-survey/
class SurveyQuestionBase(models.Model):
TEXT = 'text'
INTEGER = 'integer'
RADIO = 'radio'
SELECT = 'select'
MULTI_SELECT = 'multi-select'
ANSWER_TYPE_CHOICES = (
(INTEGER, 'Integer',),
(TEXT, 'Text',),
(RADIO, 'Radio',),
(SELECT, 'Select',),
(MULTI_SELECT, 'Multi-Select',),
)
question = models.TextField()
required = models.BooleanField()
question_type = models.CharField(choices=ANSWER_TYPE_CHOICES, max_length=20)
class Meta:
abstract = True
class SurveyResponseBase(models.Model):
"""
concrete_question_model: 'app_label.Model' - Define the concrete model this question belongs to
"""
concrete_model = 'SurveyQuestionBase'
question = models.ForeignKey(concrete_model)
response = models.TextField()
class Meta:
abstract = True
Two solutions (both working) to this problem:
The first solution involves using GenericForeignKey. The second is more interesting and involves generating the SurveyResponseBase dynamically.
Solution 1: Using GenericForeignKey
class SurveyQuestionBase(models.Model):
TEXT = 'text'
INTEGER = 'integer'
RADIO = 'radio'
SELECT = 'select'
MULTI_SELECT = 'multi-select'
ANSWER_TYPE_CHOICES = (
(INTEGER, 'Integer',),
(TEXT, 'Text',),
(RADIO, 'Radio',),
(SELECT, 'Select',),
(MULTI_SELECT, 'Multi-Select',),
)
question = models.TextField()
required = models.BooleanField()
question_type = models.CharField(choices=ANSWER_TYPE_CHOICES, max_length=20)
class Meta:
abstract = True
#classmethod
def get_subclasses(cls, *args, **kwargs):
for app_config in apps.get_app_configs():
for app_model in app_config.get_models():
model_classes = [c.__name__ for c in inspect.getmro(app_model)]
if cls.__name__ in model_classes:
yield app_model
class SurveyResponseBase(models.Model):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, limit_choices_to=get_content_choices)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
response = models.TextField()
class Meta:
abstract = True
def get_content_choices():
query_filter = None
for cls in SurveyQuestionBase.get_subclasses():
app_label, model = cls._meta.label_lower.split('.')
current_filter = models.Q(app_label=app_label, model=model)
if query_filter is None:
query_filter = current_filter
else:
query_filter |= current_filter
return query_filter
Solution 2: Dynamic base class generation
class SurveyQuestionBase(models.Model):
TEXT = 'text'
INTEGER = 'integer'
RADIO = 'radio'
RATING = 'rating'
SELECT = 'select'
MULTI_SELECT = 'multi-select'
QUESTION_TYPES = (
(INTEGER, 'Integer'),
(TEXT, 'Text'),
(RADIO, 'Radio'),
(RATING, 'Rating'),
(SELECT, 'Select'),
(MULTI_SELECT, 'Multi-Select'),
)
CHOICE_TYPES = (RADIO, RATING, SELECT, MULTI_SELECT)
question = models.TextField()
required = models.BooleanField()
question_type = models.CharField(choices=QUESTION_TYPES, max_length=20)
choices = models.TextField(blank=True, null=True)
choices.help_text = """
If the question type is "Radio," "Select," or "Multi-Select",
provide a comma-separated list of options for this question
"""
class Meta:
abstract = True
Meta = type('Meta', (object,), {'abstract': True})
def get_response_base_class(concrete_question_model):
"""
Builder method that returns the SurveyResponseBase base class
Args:
concrete_question_model: Concrete Model for SurveyQuestionBase
Returns: SurveyResponseBase Class
"""
try:
assert SurveyQuestionBase in concrete_question_model.__bases__
except AssertionError:
raise ValidationError('{} is not a subclass of SurveyQuestionBase'.format(concrete_question_model))
attrs = {
'question': models.ForeignKey(concrete_question_model, related_name='responses'),
'response': models.TextField(),
'__module__': 'survey_builder.models',
'Meta': Meta(),
}
return type('SurveyResponseBase', (models.Model,), attrs)
We decided to go ahead with Solution 2 since the GenericForeignKeys approach requires an additional ContentType selection.
I believe you can't do that because the ForeignKey doesn't know what actual model to point to.
You may be looking for GenericForeignKey (https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#generic-relations). It allows you to define that relationship properly.