Resolving a ForeignKey field pointing to the model it belongs to - python

I have a model class in Django which has a ForeignKey referencing the model it actually belongs to:
class Foo(models.Model):
name = models.CharField(max_length=256, verbose_name="Name")
#... some other fields
bar = models.ForeignKey(
"self", on_delete=models.CASCADE, null=True, blank=True
)
def __str__(self):
return self.name
I want to add a custom method in that class which resolves, on the fly, the name in a new field, e.g. bar_resolved when instantiating it in a QuerySet in a view:
from .models import Foo
foo = Foo.objects.all()
# do stuff
I've tried this:
class Foo(models.Model):
name = models.CharField(max_length=256, verbose_name="Name")
#... some other fields
bar = models.ForeignKey(
"self", on_delete=models.CASCADE, null=True, blank=True
)
# preparing the resolved bar field which should contain the 'name' value corresponding to the id:
bar_resolved = models.CharField(
max_length=256,
verbose_name="Bar name resolved",
null=True
)
def __str__(self):
return self.name
def resolve(self):
if self.bar:
self.bar_resolved = self.bar.name
return super(Foo, self).resolve()
Then in my view:
from .models import Foo
foo = Foo.objects.all()
foo.resolve()
but it raises: 'QuerySet' object has no attribute 'resolve'
How could I achieve that? and do I need to hard code a 'resolved' field in my model for that (I think it's overkill to do so)?

I do not understand why would you have a Foreing key referencing self in the database.
Instead of using resolve, you could probably do it on the save long before - i.e. when setting value of "bar"
Another idea that comes to mind is setting it in the __ init__
method of the model link to Stack
hope this helps.
def save(self, force_insert: bool = False, force_update: bool = False) -> None:
if self.field is None:
self.field = "value"
# and so on...
return super().save(force_insert, force_update)

One way is to annotate..[Django-doc] your queryset using F expressions..[Django-doc] with bar's name field:
from django.db.models import F
foos = Foo.objects.annotate(bar_resolved=F("bar__name")).all()
for foo in foos:
print(foo.bar_resolved)

Related

Set model field default to value from foreign key instance

Working on a Django app with two models (A and B), B has a field link which is a foreign key relationship to A:
# models.py
class A(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=15)
my_bool = models.BooleanField(default=True)
class B(models.Model):
link = models.ForeignKey(A)
b_bool = models.BooleanField(default=link.my_bool) # Error!
I would like for the b_bool field to have the linked my_bool value as a default if no B.b_bool is provided via graphene mutation.
Currently, using link.my_bool as a default raises the following error when making migrations:
AttributeError: 'ForeignKey' object has no attribute 'my_bool'
I don't think it will work like that. Instead, try overriding save() method:
class B(models.Model):
link = models.ForeignKey(A)
b_bool = models.BooleanField(default=False)
def save(self, *args, **kwargs):
if not self.b_bool:
self.b_bool = self.link.my_bool
super(B, self).save(*args, **kwargs)

Adapt a class method to another class

#python_2_unicode_compatible
class EmployerProfile(AbstractAddress):
customer = models.OneToOneField(
CustomerProfile, verbose_name=_('Customer'),
related_name='employerprofile')
company_name = models.CharField(_('Company name'),
max_length=50, blank=True, null=True)
...
#That function will empty all fields in this model whenever income_source
# is different of 'Employed'
def empty_fields(self):
if self.income_source != 'Employed':
to_empty = [f.name for f in EmployerProfile._meta.get_fields()]
exclude = [u'id', "has_missing_fields",
"manual_validation", "actor_actions",
"target_actions", "action_object_actions",
]
for field_name in to_empty:
if field_name not in exclude:
setattr(self, field_name, None)
#python_2_unicode_compatible
class FinancialProfile(models.Model):
customer = models.OneToOneField(
CustomerProfile, verbose_name=_('Customer'),
related_name='financialprofile')
income_source = models.CharField(_('Income source'), max_length=20,
choices=settings.LOANWOLF_INCOME_SOURCE_CHOICES,
default='employed')
I would like to move empty_field() in the class FinancialProfile. In fact, this method must emptied the attributes from EmployerProfile whenever income_source (FinancialProfile) != 'Employed'. Could anyone be able to tell me how could I do such thing?
P.S. Please tell me if there's something unclear.
Thanks!

SlugRelatedField queryset

I am struggling to figure out the queryset for SlugRelatedField.
My data is such that I have a bunch of Object instances that belong to a Project. A project has a unique 'top' Object. Objects can have the same name only if they below to different Projects.
class Object(models.Model):
project = models.ForeignKey('Project', null=False, related_name='objs')
name = models.TextField(null=False, db_index=True)
....
class Meta:
index_together = unique_together = ('project', 'name')
class Project(models.Model):
user = models.ForeignKey(get_user_model(), null=False, related_name='+')
name = models.TextField(null=False)
top = models.OneToOneField(Object, null=True, related_name='+')
....
class ObjectSerializer(NonNullSerializer):
class Meta:
model = Object
fields = ('name',)
class ProjectSerializer(NonNullSerializer):
objs = ObjectSerializer(many=True, required=False)
top = serializers.SlugRelatedField(slug_field='name', queryset=Object.objects.filter(????))
class Meta:
model = Project
fields = ('id', 'name', 'objs', 'top')
What is my queryset going to look like for top if I want to find only only the one Object that belongs to the correct Project? In other words, how to deserialize this:
[{
'name' : 'Project1',
'objs' : [{
'name': 'One'
}],
'top': 'One'
},
{
'name' : 'Project2',
'objs' : [{
'name': 'One'
}],
'top': 'One' <-- This should point to One under Project2, not One under Project1
}]
I was just revisiting my own question on this topic when I was lead back to here, so here's a way of achieving this (I think).
class ObjectSerializer(NonNullSerializer):
class Meta:
model = Object
fields = ('name',)
class TopSerializerField(SlugRelatedField):
def get_queryset(self):
queryset = self.queryset
if hasattr(self.root, 'project_id'):
queryset = queryset.filter(project_id=project_id)
return queryset
class ProjectSerializer(NonNullSerializer):
def __init__(self, *args, **kwargs):
self.project_id = kwargs.pop('project_id')
super().__init__(*args, **kwargs)
# I've needed this workaround for some cases...
# def __new__(cls, *args, **kwargs):
# """When `many=True` is provided then we need to attach the project_id attribute to the ListSerializer instance"""
# project_id = kwargs.get('project_id')
# serializer = super(ProjectSerializer, cls).__new__(cls, *args, **kwargs)
# setattr(serializer, 'project_id', project_id)
# return serializer
objs = ObjectSerializer(many=True, required=False)
top = TopSerializerField(slug_field='name', queryset=Object.objects.all())
class Meta:
model = Project
fields = ('id', 'name', 'objs', 'top')
When you go to deserialize the data, it would search for objects that belong to the correct project defined on the serializer.
I have a solution that solves this problem in my case, which I will try to explain here.
The problem, abstracted:
Suppose I have a hierarchy with Foo as the top-level objects, each associated with several Bars:
class Foo(Model):
pass
class Bar(Model):
bar_text = CharField()
foo = ForeignKey(Foo, related_name='bars')
Then I can use SlugRelatedField trivially for read only serializations of Foo, by which I mean the serializer:
class FooSerializer(ModelSerializer):
bars = serializers.SlugRelatedField(slug_field='bar_text',
many=True, read_only=True)
class Meta:
model = Foo
fields = ('bars',)
will produce serializations like:
{ 'bars' : [<bar_text>, <bar_text>, ...] }
However, this is read only. To allow writing, I have to provide a queryset class attribute outside of any methods. The problem is, because we have a Foo->Bar hierarchy, we don't know what the queryset is outside of any request. We would like to be able to override a get_queryset() method, but none seems to exist. So we can't use SlugRelatedField. What horribly hacky way can we fix it?
My Solution:
First, add an #property to the Foo model and put this property in the serializer:
In models.py:
class Foo(Model):
#property
def bar_texts(self):
return [bar.bar_text for bar in self.bars.all()]
In serializers.py:
class FooSerializer(ModelSerializer):
class Meta:
model = Foo
fields = ('bar_texts',)
This allows for the bar texts to be serialized as before, but we still can't write (we can try - the framework won't reject it but it will hit an exception when trying to save the bar_texts attribute of a Foo)
So, the hacky part - fix perform_create() in the Foo list view.
class FooList:
def perform_create(self, serializer):
# The serializer contains the bar_text field, which we want, but doesn't correspond
# to a writeable attribute of Foo. Extract the strings and save the Foo. Use pop with a default arg in case bar_texts isn't in the serialized data
bar_texts = serializer.validated_data.pop('bar_texts', [])
# Save the Foo object; it currently has no Bars associated with it
foo = serializer.save()
# Now add the Bars to the database
for bar_text in bar_texts:
foo.bars.create(bar_text=bar_text)
I hope that makes sense. It certainly works for me, but I have get to find any glaring bugs with it

django-rest-framework, multitable model inheritance, ModelSerializers and nested serializers

I can't find this info in the docs or on the interwebs.
latest django-rest-framework, django 1.6.5
How does one create a ModelSerializer that can handle a nested serializers where the nested model is implemented using multitable inheritance?
e.g.
######## MODELS
class OtherModel(models.Model):
stuff = models.CharField(max_length=255)
class MyBaseModel(models.Model):
whaddup = models.CharField(max_length=255)
other_model = models.ForeignKey(OtherModel)
class ModelA(MyBaseModel):
attr_a = models.CharField(max_length=255)
class ModelB(MyBaseModel):
attr_b = models.CharField(max_length=255)
####### SERIALIZERS
class MyBaseModelSerializer(serializers.ModelSerializer):
class Meta:
model=MyBaseModel
class OtherModelSerializer(serializer.ModelSerializer):
mybasemodel_set = MyBaseModelSerializer(many=True)
class Meta:
model = OtherModel
This obviously doesn't work but illustrates what i'm trying to do here.
In OtherModelSerializer, I'd like mybasemodel_set to serialize specific represenntations of either ModelA or ModelB depending on what we have.
If it matters, I'm also using django.model_utils and inheritencemanager so i can retrieve a queryset where each instance is already an instance of appropriate subclass.
Thanks
I've solved this issue a slightly different way.
Using:
DRF 3.5.x
django-model-utils 2.5.x
My models.py look like this:
class Person(models.Model):
first_name = models.CharField(max_length=40, blank=False, null=False)
middle_name = models.CharField(max_length=80, blank=True, null=True)
last_name = models.CharField(max_length=80, blank=False, null=False)
family = models.ForeignKey(Family, blank=True, null=True)
class Clergy(Person):
category = models.IntegerField(choices=CATEGORY, blank=True, null=True)
external = models.NullBooleanField(default=False, null=True)
clergy_status = models.ForeignKey(ClergyStatus, related_name="%(class)s_status", blank=True, null=True)
class Religious(Person):
religious_order = models.ForeignKey(ReligiousOrder, blank=True, null=True)
major_superior = models.ForeignKey(Person, blank=True, null=True, related_name="%(class)s_superior")
class ReligiousOrder(models.Model):
name = models.CharField(max_length=255, blank=False, null=False)
initials = models.CharField(max_length=20, blank=False, null=False)
class ClergyStatus(models.Model):
display_name = models.CharField(max_length=255, blank=True, null=True)
description = models.CharField(max_length=255, blank=True, null=True)
Basically - The base model is the "Person" model - and a person can either be Clergy, Religious, or neither and simply be a "Person". While the models that inherit Person have special relationships as well.
In my views.py I utilize a mixin to "inject" the subclasses into the queryset like so:
class PersonSubClassFieldsMixin(object):
def get_queryset(self):
return Person.objects.select_subclasses()
class RetrievePersonAPIView(PersonSubClassFieldsMixin, generics.RetrieveDestroyAPIView):
serializer_class = PersonListSerializer
...
And then real "unDRY" part comes in serializers.py where I declare the "base" PersonListSerializer, but override the to_representation method to return special serailzers based on the instance type like so:
class PersonListSerializer(serializers.ModelSerializer):
def to_representation(self, instance):
if isinstance(instance, Clergy):
return ClergySerializer(instance=instance).data
elif isinstance(instance, Religious):
return ReligiousSerializer(instance=instance).data
else:
return LaySerializer(instance=instance).data
class Meta:
model = Person
fields = '__all__'
class ReligiousSerializer(serializers.ModelSerializer):
class Meta:
model = Religious
fields = '__all__'
depth = 2
class LaySerializer(serializers.ModelSerializer):
class Meta:
model = Person
fields = '__all__'
class ClergySerializer(serializers.ModelSerializer):
class Meta:
model = Clergy
fields = '__all__'
depth = 2
The "switch" happens in the to_representation method of the main serializer (PersonListSerializer). It looks at the instance type, and then "injects" the needed serializer. Since Clergy, Religious are all inherited from Person getting back a Person that is also a Clergy member, returns all the Person fields and all the Clergy fields. Same goes for Religious. And if the Person is neither Clergy or Religious - the base model fields are only returned.
Not sure if this is the proper approach - but it seems very flexible, and fits my usecase. Note that I save/update/create Person thru different views/serializers - so I don't have to worry about that with this type of setup.
I was able to do this by creating a custom relatedfield
class MyBaseModelField(serializers.RelatedField):
def to_native(self, value):
if isinstance(value, ModelA):
a_s = ModelASerializer(instance=value)
return a_s.data
if isinstance(value, ModelB):
b_s = ModelBSerializer(instance=value)
return b_s.data
raise NotImplementedError
class OtherModelSerializer(serializer.ModelSerializer):
mybasemodel_set = MyBaseModelField(many=True)
class Meta:
model = OtherModel
fields = # make sure we manually include the reverse relation (mybasemodel_set, )
I do have concerns that instanting a Serializer for each object is the reverse relation queryset is expensive so I'm wondering if there is a better way to do this.
Another approach i tried was dynamically changing the model field on MyBaseModelSerializer inside of __init__ but I ran into the issue described here:
django rest framework nested modelserializer
Using Django 3.1, I found that it is possible to override get_serializer instead of get_serializer_class, in which case you can access the instance as well as self.action and more.
By default get_serializer will call get_serializer_class, but this behavior can be adjusted to your needs.
This is cleaner and easier than the solutions proposed above, so I'm adding it to the thread.
Example:
class MySubclassViewSet(viewsets.ModelViewSet):
# add your normal fields and methods ...
def get_serializer(self, *args, **kwargs):
if self.action in ('list', 'destroy'):
return MyListSerializer(args[0], **kwargs)
if self.action in ('retrieve', ):
instance = args[0]
if instance.name.contains("really?"): # or check if instance of a certain Model...
return MyReallyCoolSerializer(instance)
else return MyNotCoolSerializer(instance)
# ...
return MyListSerializer(*args, **kwargs) # default
I'm attempting to use a solution that involves different serializer subclasses for the different model subclasses:
class MyBaseModelSerializer(serializers.ModelSerializer):
#staticmethod
def _get_alt_class(cls, args, kwargs):
if (cls != MyBaseModel):
# we're instantiating a subclass already, use that class
return cls
# < logic to choose an alternative class to use >
# in my case, I'm inspecting kwargs["data"] to make a decision
# alt_cls = SomeSubClass
return alt_cls
def __new__(cls, *args, **kwargs):
alt_cls = MyBaseModel.get_alt_class(cls, args, kwargs)
return super(MyBaseModel, alt_cls).__new__(alt_cls, *args, **kwargs)
class Meta:
model=MyBaseModel
class ModelASerializer(MyBaseModelSerializer):
class Meta:
model=ModelA
class ModelBSerializer(MyBaseModelSerializer):
class Meta:
model=ModelB
That is, when you try and instantiate an object of type MyBaseModelSerializer, you actually end up with an object of one of the subclasses, which serialize (and crucially for me, deserialize) correctly.
I've just started using this, so it's possible that there are problems I've not run into yet.
I found this post via Google trying to figure out how to handle multiple table inheritance without having to check the model instance type. I implemented my own solution.
I created a class factory and a mixin to generate the serializers for the child classes with the help of InheritanceManger from django-model-utils.
models.py
from django.db import models
from model_utils import InheritanceManager
class Place(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=80)
# Use the InheritanceManager for select_subclasses()
objects = InheritanceManager()
class Restaurant(Place):
serves_hot_dogs = models.BooleanField(default=False)
serves_pizza = models.BooleanField(default=False)
serializers.py
from rest_framework import serializers
from .models import Location
def modelserializer_factory(model, class_name='ModelFactorySerializer',
meta_cls=None, **kwargs):
"""Generate a ModelSerializer based on Model"""
if meta_cls is None:
# Create a Meta class with the model passed
meta_cls = type('Meta', (object,), dict(model=model))
elif not hasattr(meta_cls, 'model'):
# If a meta_cls is provided but did not include a model,
# set it to the model passed into this function
meta_cls.model = model
# Create the ModelSerializer class with the Meta subclass
# we created above; also pass in any additional keyword
# arguments via kwargs
ModelFactorySerializer = type(class_name, (serializers.ModelSerializer,),
dict(Meta=meta_cls, **kwargs))
ModelFactorySerializer.__class__.__name__ = class_name
return ModelFactorySerializer
class InheritedModelSerializerMixin:
def to_representation(self, instance):
# Get the model of the instance
model = instance._meta.model
# Override the model with the inherited model
self.Meta.model = model
# Create the serializer via the modelserializer_factory
# This will use the name of the class this is mixed with.
serializer = modelserializer_factory(model, self.__class__.__name__,
meta_cls=self.Meta)
# Instantiate the Serializer class with the instance
# and return the data
return serializer(instance=instance).data
# Mix in the InheritedModelSerializerMixin
class LocationSerializer(InheritedModelSerializerMixin, serializers.ModelSerializer):
class Meta:
model = Location # 'model' is optional since it will use
# the instance's model
exclude = ('serves_pizza',) # everything else works as well
depth = 2 # including depth
views.py
from .models import Location
from .serializers import LocationSerializer
# Any view should work.
# This is an example using viewsets.ReadOnlyModelViewSet
# Everything else works as usual. You will need to chain
# ".select_subclasses()" to the queryset to select the
# child classes.
class LocationViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Location.objects.all().select_subclasses()
serializer_class = LocationSerializer

Django can't find the right queryset

class TimeStampedModel(models.Model):
created = DateTimeField(_('created'), auto_now=False, auto_now_add=True)
modified = DateTimeField(_('modified'), auto_now=True, auto_now_add=False)
class Meta:
abstract = True
class TimeFramedModel(models.Model):
start = models.DateTimeField(_('start'), null=True, blank=True)
end = models.DateTimeField(_('end'), null=True, blank=True)
class Meta:
abstract = True
class EntryQueryset(QuerySet):
def published(self):
return self.filter(self.status == 'published')
class EntryManger(models.Manager):
def get_query_set(self):
print 'using right custom manager'
return EntryQueryset(self.model, using=self._db)
class Entry(TimeStampedModel, TimeFramedModel):
status = models.CharField(
_('status'), choices=STATUS_CHOICES, max_length=16,
default='draft')
objects = EntryManger()
When I do Entry.objects.published(), the error raises. It complains that
'EntryManger' object has no attribute 'published',
The text of 'using right custom manager' wasn't printed.
What could cause this error? Thank you!
Entry.objects is a manager, not a queryset. You put published inside your custom queryset, so you need to do Entry.objects.all().published() or Entry.objects.get_queryset().published()
You also aren't implementing published correctly. It should be more like:
class EntryQueryset(QuerySet):
def published(self):
return self.filter(status='published')
Your method published() should be on the EntryManager class, there is no need to create a new QuerySet class (generally that is only when you want to dig deeper into how data is fetched from the database).
The correct solution would be:
class EntryManger(models.Manager):
def published(self):
print 'using right custom manager'
return self.filter(self.status == 'published')
You can delete the EntryQuerySet class entirely.

Categories

Resources