How to rename ForeignKey set field in Django Rest Framework - python

I am serializing Foreign key set using Django Rest Framework, I have following models:
class Transaction(models.Model):
...
class TransactionStatus(models.Model):
transaction = models.ForeignKey(Transaction)
...
I have a serializer for both of these models, one of them looks like this:
class TransactionSerializer(serializers.ModelSerializer):
transactionstatus_set = TransactionStatusSerializer(many=True, read_only=True)
class Meta:
model = Transaction
depth = 1
fields = ('id', 'transactionstatus_set')
I want to have here a list of transaction statuses from back referenced _set queryset... But transaction_set just seems very awkward name in API for that..

After a quick experimenting I have discovered that this will do the trick:
class TransactionSerializer(serializers.ModelSerializer):
changes = TransactionStatusSerializer(many=True, read_only=True, source='transactionstatus_set')
class Meta:
model = Transaction
depth = 1
fields = ('id', 'changes')
Now I have a list of the statuses linked by foreign key with a nice name...

Related

Django using id to write foreign key fields

I need to have a CRUD interface for interconnected django models. Can i use _id fields to fill in ForeignKey fields? i.e. will this work:
class MyModel(models.Model):
foreign_key = ForeignKey('AnotherModel')
class MyModelSerializer(serializers.ModelSerializer):
foreign_key_id = serializers.IntegerField(write_only=True)
foreign_key = AnotherModelSerializer(read_only=True)
class Meta:
model = MyModel
fields = ('foreign_key', 'foreign_key_id')

Django prefetch_related in property of inherited model

I am running into an issue with prefetch_related on a property of an inherited model.
In models.py I have:
class Security(models.Model):
name = models.TextField()
...other fields
class Fund(Security):
...other fields
#property
def underlying_security(self):
return self.maps_when_mapped.get().original_security
class SecurityToSecurityMap(models.Model):
original_security = models.ForeignKey(Security, related_name="maps_when_original")
mapped_security = models.ForeignKey(Security, related_name="maps_when_mapped")
and in serializers.py
class UnderlyingSecuritySerializer(serializers.Serializer):
id = serializers.IntegerField()
name = serializers.CharField()
class FundSerializer(serializers.ModelSerializer):
underlying_security = UnderlyingSecuritySerializer(read_only=True, allow_null=True)
class Meta:
model = Fund
fields = ("id", "name", "underlying_security")
Now, I when I am querying fund objects:
queryset = Fund.objects.prefetch_related("maps_when_mapped__original_security")
Django ignores prefetching. Experimented with `Prefetch, tried moving property to the parent - no luck, still getting hundreds on queries. Any way this can be optimised?
I am using Django 2.2 with Postgres 11
UPDATE 1
After looking at the question, I suspect that problem is when I use .get() in property, it is immediately evaluated but then not sure how to fetch it without evaluation

DRF: Reverse foreign key assignment using an array of primary keys

As stated in this question
With Django REST Framework, a standard ModelSerializer will allow ForeignKey model relationships to be assigned or changed by POSTing an ID as an Integer.
I am attempting to update a reverse relationship of the following format:
class Lesson(models.Model):
name = models.TextField()
class Quiz(models.Model):
lesson = models.ForeignKey(Lesson, related_name='quizzes', null=True)
class LessonSerializer(serializers.ModelSerializer):
quizzes = serializers.PrimaryKeyRelatedField(queryset=Quiz.objects.all(), many=True, write_only=True)
class Meta:
model = Lesson
fields = ('quizzes')
When posting an update containing an array of quiz primary keys using LessonSerializer I get TypeError: 'Quiz' instance expected, got '1'.
Is it possible to assign or change a reverse relationship by POSTing an array of primary keys?
You don't need special field in a serializer, DRF serializers is smart enough to figure out related field from a fields value.
class LessonSerializer(serializers.ModelSerializer):
class Meta:
model = Lesson
fields = ('quizzes', 'name')
And you should pass list of ids, if there is only one value it should be a list anyway.
To solve this you need to create a Quiz instance first before you assign it to Lesson. Below's a small change to your code.
class Lesson(models.Model):
name = models.TextField()
class Quiz(models.Model):
lesson = models.ForeignKey(Lesson, related_name='quizzes', null=True)
class LessonSerializer(serializers.ModelSerializer):
quizzes = serializers.PrimaryKeyRelatedField(queryset=Quiz.objects.all(), many=True, write_only=True)
class Meta:
model = Lesson
fields = ('quizzes')
class QuizSerializer(serializers.ModelSerializer):
class Meta:
model = Quiz
fields = ('name')
Create POST request with with quiz then run your post again

django: prevent UniqueTogetherValidator on Serializer

I'm having a problem with django. I created my model with some fields like so:
class MyModel(Model):
field1 = models.IntegerField()
field2 = models.CharField(max_length=200)
field3 = models.IntegerField()
class Meta:
unique_together = (('field1', 'field2'))
And I have an API route, which links to a model serializer:
class MyModelSerializer(ModelSerializer):
class Meta:
model = models.MyModel
The problem is that when I send objects via the API to update them, the serializer raises {'non_field_errors': ['The fields field1, field2 must make a unique set.']}
Does anyone know how to prevent UniqueTogetherValidator to be used on the serializer?
This is based off of djangorestframework==3.8.2.
Override the get_validators method that is on ModelSerializer and comment out the call for getting the unique_together_validators if that is the only validator you really care about omitting. Otherwise this answer https://stackoverflow.com/a/46022282/1658745 suggests just settings validators to empty list to turn off all validation for that serializer.
class MyModelSerializer(ModelSerializer):
def get_validators(self):
"""
Determine the set of validators to use when instantiating serializer.
"""
# If the validators have been declared explicitly then use that.
validators = getattr(getattr(self, 'Meta', None), 'validators', None)
if validators is not None:
return validators[:]
# Otherwise use the default set of validators.
return (
# self.get_unique_together_validators() +
self.get_unique_for_date_validators()
)
class Meta:
model = models.MyModel

Foreign key value in Django REST Framework

models.py:
class Station(models.Model):
station = models.CharField()
class Flat(models.Model):
station = models.ForeignKey(Station, related_name="metro")
# another fields
Then in serializers.py:
class StationSerializer(serializers.ModelSerializer):
station = serializers.RelatedField(read_only=True)
class Meta:
model = Station
class FlatSerializer(serializers.ModelSerializer):
station_name = serializers.RelatedField(source='station', read_only=True)
class Meta:
model = Flat
fields = ('station_name',)
And I have an error:
NotImplementedError: RelatedField.to_representation() must be implemented.
If you are upgrading from REST framework version 2 you might want ReadOnlyField.
I read this, but it does not help me.
How to fix that?
Thanks!
RelatedField is the base class for all fields which work on relations. Usually you should not use it unless you are subclassing it for a custom field.
In your case, you don't even need a related field at all. You are only looking for a read-only single foreign key representation, so you can just use a CharField.
class StationSerializer(serializers.ModelSerializer):
station = serializers.CharField(read_only=True)
class Meta:
model = Station
class FlatSerializer(serializers.ModelSerializer):
station_name = serializers.CharField(source='station.name', read_only=True)
class Meta:
model = Flat
fields = ('station_name', )
You also appear to want the name of the Station object in your FlatSerializer. You should have the source point to the exact field, so I updated it to station.name for you.

Categories

Resources