Django Rest Framework read/write ModelSerializer with ChoiceField - django

I have a few field in my model "OrderItem" that are ChoiceFields or Enums. I want to represent the label of the choice and doing it with serializers.CharField(source='get_service_display'). See also Django Rest Framework with ChoiceField.
That works fine, but creating objects is not working anymore with following error message OrderItem() got an unexpected keyword argument 'get_service_display'
Here is my model
class OrderItem(TimeStampedModel):
class Categories(models.IntegerChoices):
GRASS = (1, _('grass'))
order = models.ForeignKey(Order, related_name='order_items', on_delete=models.CASCADE)
unit_price = models.DecimalField(max_digits=6, decimal_places=2)
category = models.IntegerField(choices=Categories.choices)
and here my serializer
class OrderItemSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(required=False)
category = serializers.CharField(source='get_category_display')
class Meta:
model = OrderItem
fields = ['id', 'unit_price', 'category']
read_only_fields = ['unit_price']
how can I create a model like this?

Try this:
from rest_framework import fields
from your_app_path.models import Categories
class OrderItemSerializer(serializers.ModelSerializer):
category = fields.ChoiceField(Categories.choices)
One other note. I'm not familiar with sub-classing models just for choices. I usually do it this way:
class OrderItem(TimeStampedModel):
GRASS = 'grass'
CATEGORY_CHOICES = (
(1, _(GRASS)),
)
order = models.ForeignKey(Order, related_name='order_items', on_delete=models.CASCADE)
unit_price = models.DecimalField(max_digits=6, decimal_places=2)
category = models.IntegerField(choices=CATEGORY_CHOICES)

Related

Getting the whole object of a related field in django

I have a model like this:
class Cart(models.Model):
id = models.UUIDField(primary_key=True, default=uuid4)
customer = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True)
class Reception(models.Model):
PAYMENT_STATUS_PENDING = 'P'
PAYMENT_STATUS_COMPLETE = 'C'
PAYMENT_STATUS_FAILED = 'F'
PAYMENT_STATUS_CHOICES = [
(PAYMENT_STATUS_PENDING, 'Pending'),
(PAYMENT_STATUS_COMPLETE, 'Complete'),
(PAYMENT_STATUS_FAILED, 'Failed')
]
cart = models.ForeignKey(Cart, on_delete=models.CASCADE, null=True)
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
entry_date = models.DateField()
total_price = models.IntegerField()
payment_status = models.CharField(
max_length=1,
choices=PAYMENT_STATUS_CHOICES,
default=PAYMENT_STATUS_PENDING
)
My question is:
How can I get a particular Cart record from the Reception model?
I tried using this serializer:
class ReceptionSerializer(serializers.ModelSerializer):
class Meta:
model = Reception
fields = ['id', 'customer', 'entry_date', 'payment_status', 'cart']
but it only returns the id of a cart. I want to return the whole object of that specific cart.
How can I do that?
If you want to utilize nested serialization, one possible solution would be to define CartSerializer and override the cart field of the ReceptionSerializer class as follows
class CartSerializer(serializers.ModelSerializer):
class Meta:
model = Cart
fields = ['id', 'customer']
class ReceptionSerializer(serializers.ModelSerializer):
cart = CartSerializer(many=True)
class Meta:
model = Reception
fields = ['id', 'customer', 'entry_date', 'payment_status', 'cart']
However, please note that the cart field will become read-only this way.

How to use prefetch_related in django rest api with foreying key and heritage

I am working on this Django project, it uses heritage and foreign keys for its models.
These are the models:
class SetorFii(models.Model):
name = models.CharField(max_length=255)
class Asset(models.Model):
category = models.ForeignKey(
Category, related_name='categories', on_delete=models.CASCADE)
ticker = models.CharField(max_length=255, unique=True)
price = models.FloatField()
class Fii(Asset):
setor_fii = models.ForeignKey(
SetorFii, null=True, default=None, on_delete=models.CASCADE, related_name="setor_fiis")
Class Crypto(Asset):
circulating_supply = models.FloatField(default=0)
class PortfolioAsset(models.Model):
asset = models.ForeignKey(Asset, on_delete=models.CASCADE)
I would like to get the field setor_fii in the PortfolioAssetSerializer, That is what I tried without success.
I get this error message: Cannot find 'setor_fii' on PortfolioAsset object, 'setor_fii' is an invalid parameter to prefetch_related()
Would like some help to achieve that.
The serializer:
class PortfolioAssetSerializer(serializers.ModelSerializer):
category = serializers.CharField(source='asset.category.name')
setor_fii = serializers.CharField(source='asset.setor_fii.name')
class Meta:
model = models.PortfolioAsset
fields = (
'id',
'category',
'setor_fii'
)
The view
class PortfolioAssetList(generics.ListAPIView):
serializer_class = serializers.PortfolioAssetSerializer
def get_queryset(self):
return models.PortfolioAsset.objects.filter(portfolio_id=self.kwargs['pk']).prefetch_related('setor_fii')
To prefetch setor_fii, you will have to go through asset. Since Fii inherits from Asset, Asset will have an automatically created one-to-one field named fii. You can then use that to access setor_fii:
PortfolioAsset.objects.filter(
portfolio_id=self.kwargs['pk'],
).prefetch_related(
'asset__fii__setor_fii',
)
Also since the whole relationships here are just one to ones, you can use select_related instead of prefetch_related to get them all in one query (compared to three queries using prefetch_related):
PortfolioAsset.objects.filter(
portfolio_id=self.kwargs['pk'],
).select_related(
'asset__fii__setor_fii',
)

How to display many fields' values with ForeignKey relationship?

Looking for solution of this problem I encountered some similar threads, but referring to older versions of Django/DRF and thus not working in my case.
There are these two models:
class CsdModel(models.Model):
model_id = models.CharField("Item ID", max_length=8, primary_key=True)
name = models.CharField("Item Name", max_length=40)
active = models.BooleanField(default=True)
def __str__(self):
return self.model_id
class CsdListing(models.Model):
model_id = models.ForeignKey(CsdModel, on_delete=models.CASCADE, default=0, related_name='m_id')
name = models.ForeignKey(CsdModel, on_delete=models.CASCADE, default=0, related_name='m_name')
(...)
EDIT: Serializers are defined this way:
class CsdModelSerializer(serializers.ModelSerializer):
model_id = serializers.RegexField(regex='^\w{2}\d{3}$', allow_blank=False)
name = serializers.CharField(min_length=6, max_length=50, allow_blank=False)
class Meta:
model = CsdModel
fields = '__all__'
class CsdListingSerializer(serializers.ModelSerializer):
session_id = serializers.RegexField(regex='^s\d{2}$', allow_blank=False)
def validate_session_id(self, value):
(...)
class Meta:
model = CsdListing
fields = '__all__'
What I'd like to see, is model_id and name from CsdModel displayed inside a form created based on CsdListing model. But instead, the ID is duplicated:
How should I rebuild the model(s) to have both ID and name displayed in the form?
You should have only one foreign key. But the listing serializer should then reference the model as a nested serializer.
class CsdListing(models.Model):
model = models.ForeignKey(CsdModel, on_delete=models.CASCADE, default=0, related_name='listing')
class CsdListingSerializer(serializers.ModelSerializer):
model = CsdModelSerializer()
session_id = serializers.RegexField(regex='^s\d{2}$', allow_blank=False)

Django Rest Framework - how to get only one field from related models set

I have following models:
from django.db import models
class City(models.Model):
name = models.CharField(max_length=30)
last_update = models.DateTimeField(null=True)
class BusStop(models.Model):
city = models.ForeignKey(City, on_delete=models.CASCADE)
name = models.CharField(max_length=200, blank=True, default='')
Now using Django Rest Framework, I would like to create serializer that will return City details along with the list of all BusStops in the city - but I want the list to be only strings with BusStop names, like this:
{
"id": 1
"name": "City"
"last_update": "2019-09-19T22:13:54.851363Z"
"bus_stops": [
"stop1",
"stop2",
"stop3"
]
}
What I've tried so far is following serializers:
from rest_framework import serializers
class BusStopSerializer(serializers.ModelSerializer):
class Meta:
model = BusStop
fields = ('name', )
class CityDetailsSerializer(serializers.ModelSerializer):
busstop_set = BusStopSerializer(many=True)
class Meta:
model = City
fields = ('id', 'name', 'last_update', 'busstop_set')
But this creates list of objects with 'name' in them. So, how can I create a list with only BusStop names (as strings) in it?
Instead of the extra BusStopSerializer you could use a StringRelatedField:
# models.py
class BusStop(models.Model):
city = models.ForeignKey(City, on_delete=models.CASCADE)
name = models.CharField(max_length=200, blank=True, default='')
def __str__(self):
return self.name
# serializers.py
class CityDetailsSerializer(serializers.ModelSerializer):
bus_stops = StringRelatedField(many=True)
class Meta:
model = City
fields = ('id', 'name', 'last_update', 'bus_stops')
StringRelatedField, as recommended by wfehr, will only work as long as the __str__ method of BusStop only returns the name. An alternative is to use SlugRelatedField which allows you to specify a particular field from the related model, and has no dependency on __str__.
bus_stops = SlugRelatedField(many=True, slug_field='name')

Django Rest Serializer: Reverse relationships

Disclaimer: I am learning django as I apply it to a database / PHP app i inherited. The database is a bit of a mess, with no foreign key constrains, and inconsistent naming. I do not want to touch or redo anything on the database because I don't want to mes with the legacy application at all.
Stack: Python 2.7. Django 1.5, Django Rest Framework
The problem is that I have a relationship where there is an Idea that has multiple Tickers. The tickers table has the foreign key to the ideas (teaser_id) so that we have something like
**Tickers**
id teaser_id
1 1
2 1
3 1
4 2
4 2
**Ideas**
id
1
2
I had django generate the model from the database, but without the FK Constraints it didn't generate all the relationships properly. So here is haw the models are configured:
class Tickers(models.Model):
id = models.IntegerField(primary_key=True)
# I changed to this
teaser_id = models.ForeignKey(Idea)
# From
# teaser_id = models.IntegerField(null=True, blank=True)
ticker = models.CharField(max_length=135L, blank=True)
date_added = models.CharField(max_length=135L, blank=True)
class Meta:
db_table = 'teaser_tickers'
class Idea(models.Model):
id = models.IntegerField(primary_key=True)
industry_id = models.IntegerField()
post_type = models.CharField(max_length=45L)
class Meta:
db_table = 'idea'
Here are my serializers
class TickerSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = myModels.Tickers
fields = (
'id'
,'teaser_id'
,'ticker'
)
class IdeaSerializer(serializers.HyperlinkedModelSerializer):
user = UserSerializer(many=False, read_only=True)
ticker = TickerSerializer(many=True, read_only=True, )
teaser = myFields.TeaserField(teaser_length=200, original_field='content')
class Meta:
model = myModels.Idea
fields = (
'id'
, 'title'
, 'date_added'
, 'user'
, 'teaser'
, 'ticker'
)
I want the Ideas Resource to return the tickers as a child node set.
The REST request is for the Idea where the tickers is a child element. So I am getting an exception that ticker isn't defined in idea. Fine get that - but I am just guessing on how to set this up at this point - I am sludging through documentation and source - but was hoping someone could help me out.
THank you
As akaphenom said you have to use the related_name in your serializer, but since you don't specify any in your models you must use the default, in this case teaser_set and your IdeaSerializer must be:
class IdeaSerializer(serializers.HyperlinkedModelSerializer):
user = UserSerializer(many=False, read_only=True)
tickers = ReverseTickerSerializer(many=True, read_only=True)
teaser_set = myFields.TeaserField(teaser_length=200, original_field='content')
class Meta:
model = myModels.Idea
fields = (
'id',
'title',
'date_added',
'user',
'teaser_set',
'tickers',
)
SO the solution for the reverse lookup is specifying the model correctly, and namely the related_name which is the field that is created in the foreign model to perform the reverse look up. Now I specified a custom serializer to limit to the content I am interested in - but that piece is optional.
class Tickers(models.Model):
id = models.IntegerField(primary_key=True)
# I changed to this
teaser_id = models.ForeignKey(Idea, related_name='tickers')
# From
# teaser_id = models.IntegerField(null=True, blank=True)
ticker = models.CharField(max_length=135L, blank=True)
date_added = models.CharField(max_length=135L, blank=True)
class Meta:
db_table = 'teaser_tickers'
class Idea(models.Model):
id = models.IntegerField(primary_key=True)
industry_id = models.IntegerField()
post_type = models.CharField(max_length=45L)
class Meta:
db_table = 'idea'
Here are my serializers
class ReverseTickerSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = myModels.Tickers
fields = (
'id'
,'ticker'
)
class IdeaSerializer(serializers.HyperlinkedModelSerializer):
user = UserSerializer(many=False, read_only=True)
tickers = ReverseTickerSerializer(many=True, read_only=True)
teaser = myFields.TeaserField(teaser_length=200, original_field='content')
class Meta:
model = myModels.Idea
fields = (
'id'
, 'title'
, 'date_added'
, 'user'
, 'teaser'
, 'tickers'
)