i've created the models and serializers as below
serializers.py
class ProductCategorySerializer(ModelSerializer):
class Meta:
model = ProductCategory
fields = ['id', 'name']
#staticmethod
def get_product_category_name(obj):
return obj.product_categories.name
class ProductSerializer(ModelSerializer):
product_categories = ProductCategorySerializer
class Meta:
model = Product
fields = ['id', 'name', 'default_price', 'description', 'product_categories']
models.py
class ProductCategory(models.Model):
class Meta:
db_table = 'itw_product_category'
verbose_name = 'product category'
verbose_name_plural = 'product categories'
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Product(models.Model):
class Meta:
db_table = 'itw_product'
verbose_name = 'product'
verbose_name_plural = 'products'
name = models.CharField(max_length=50)
default_price = models.FloatField(max_length=10)
description = models.TextField(max_length=50)
deleted = models.BooleanField(default=False)
product_categories = models.ManyToManyField(ProductCategory, verbose_name='product_categories', related_name='products')
Now, when i try to create a new model, product_categories returns only the id and not the name, what should i change?
Example:
{
"id": 25,
"name": "kms",
"default_price": 932.0,
"description": "kms",
"product_categories": [
5
]
}
You can display the string value in manytomanyfield fields with this method.
However, since you can update it with its numeric value when you want to update it, you will need to use a method or a separate serializer and view for updating, deleting and creating data to view the data.
class ProductCategory(models.Model):
product_categories = StringRelatedField(many=True)
class Meta:
db_table = 'itw_product_category'
verbose_name = 'product category'
verbose_name_plural = 'product categories'
name = models.CharField(max_length=50)
def __str__(self):
return self.name
Hope this can help you. Also, please read nested relationship.
class ProductSerializer(ModelSerializer):
product_categories = ProductCategorySerializer(many=True)
Related
I want to access verbose name of models to put it as a key in serializer. But I can't find a way to do it.
My models are:
class ProductCategory(models.Model):
name = models.CharField(max_length=150, unique=True)
created_at = models.DateTimeField(default=timezone.now)
modified_at = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.name
class DeviceTypeCategory(ProductCategory):
product_category = models.ForeignKey(ProductCategory, on_delete=models.CASCADE,
related_name="device_types")
class Meta:
verbose_name = _("Device type")
verbose_name_plural = _("Device types")
class DeviceBrandCategory(ProductCategory):
product_category = models.ForeignKey(ProductCategory, on_delete=models.CASCADE,
related_name="device_brands")
class PartTypeCategory(ProductCategory):
product_category = models.ForeignKey(ProductCategory, on_delete=models.CASCADE, related_name="part_types")
And my serializer:
class ProductCategorySerializer(serializers.ModelSerializer):
device_types = serializers.StringRelatedField(many=True)
device_brands = serializers.StringRelatedField(many=True)
part_types = serializers.StringRelatedField(many=True)
class Meta:
model = ProductCategory
fields = ('name', 'device_types', 'device_brands', 'part_types')
Any suggestions would help. I would also be glad to hear out other ideas on how to create categories model. I've tried django-mptt, but I need product to belong to multiple subcategories. The django-polymorphic-mptt could have help. But I couldn't find proper documentation.
You can do something like the following:
class ProductCategorySerializer(serializers.ModelSerializer):
device_types = serializers.StringRelatedField(many=True)
device_brands = serializers.StringRelatedField(many=True)
part_types = serializers.StringRelatedField(many=True)
plural_name = serializers.SerializerMethodField()
def get_plural_name(self, obj):
return ProductCategory._meta.verbose_name_plural
class Meta:
model = ProductCategory
fields = ('name', 'device_types', 'device_brands', 'part_types', 'plural_name')
I wanted to save multiple relational objects in a foreign key but unfortunately, I'm getting an error which I attached below. I already have the object with ID of 189 in my DB
error:
{
"tags": [
"Invalid pk \"189\" - object does not exist."
]
}
views.py
queryset = PackageRoom.objects.all()
serializer = PackageRoomSerializer(queryset, many=True)
return Response(serializer.data)
serializers.py
class PackageRoomSerializer(serializers.ModelSerializer):
tags = serializers.PrimaryKeyRelatedField(queryset=PackageRoom.objects.all(), many=True)
class Meta:
model = PackageRoom
fields = ['id', 'name', 'description', 'tags']
models.py
class Tag(models.Model):
name = models.CharField(max_length=255, default='')
description = models.CharField(max_length=255, default='')
singleline = models.ManyToManyField(Singleline)
class Meta:
db_table = 'tags'
class PackageRoom(models.Model):
name = models.CharField(max_length=255, default='')
tags = models.ForeignKey(Tag, on_delete=models.PROTECT)
class Meta:
db_table = 'package_rooms'
It was a silly mistake that I was doing there that I was passing the PackageRoom model in my serializer but I need to pass the Tag model instead.
class PackageRoomSerializer(serializers.ModelSerializer):
tags = serializers.PrimaryKeyRelatedField(queryset=Tag.objects.all(), many=True)
class Meta:
model = PackageRoom
fields = ['id', 'name', 'description', 'tags']
I am learning Django so this is all very new to me. What I am trying to create is a bit of functionality in my admin panel that will allow me to create a layout like this.
Test
-Event1
--Property1
--Property2
--Property3
--Property4
-Event2
--Property1a
--Property2b
--Property3c
--Property4d
-Event3
--Property1aa
--Property2bb
--Property3cc
--Property4dd
-Event4
--Property1aaa
--Property2bbb
--Property3ccc
--Property4ddd
I want to have multiple tests. My current model setup looks like this:
from django.db import models
from django.forms import ModelForm
TYPE_CHOICES = (
("string", "string"),
("integer", "integer"),
("array", "array"),
("boolean", "boolean")
)
class Test(models.Model):
name = models.CharField(max_length=255)
description = models.CharField(max_length=255, blank=True)
class Meta:
verbose_name = 'Test'
verbose_name_plural = 'Tests'
def __str__(self):
return self.name
class Event(models.Model):
name = models.CharField(max_length=255)
test_id = models.IntegerField()
class Meta:
verbose_name = 'Event'
verbose_name_plural = 'Events'
def __str__(self):
return self.name
class Property(models.Model):
name = models.CharField(max_length=255)
property_type = models.CharField(max_length=20, choices=TYPE_CHOICES)
expected_value = models.CharField(max_length=255)
class Meta:
verbose_name = 'Property'
verbose_name_plural = 'Properties'
def __str__(self):
return self.name
class TestForm(ModelForm):
class Meta:
model = Test
fields = ['name', 'description']
I have my admin panel setup so that I can create multiple properties. But then when I go to the "Events" section in my admin panel I can only create events. I want to be able to pick the properties and add them to my event. Then I want to be able to go to the Test page and add the events to it.
A good example of what I am trying to create is a replica of this: http://jsonparser.tools/tests.php
you should define foreign keys for events and properties:
from django.db import models
from django.forms import ModelForm
TYPE_CHOICES = (
("string", "string"),
("integer", "integer"),
("array", "array"),
("boolean", "boolean")
)
class Test(models.Model):
name = models.CharField(max_length=255)
description = models.CharField(max_length=255, blank=True)
class Meta:
verbose_name = 'Test'
verbose_name_plural = 'Tests'
def __str__(self):
return self.name
class Event(models.Model):
name = models.CharField(max_length=255)
test = models.ForeignKey(Test,on_delete=models.CASCADE)
class Meta:
verbose_name = 'Event'
verbose_name_plural = 'Events'
def __str__(self):
return self.name
class Property(models.Model):
event = models.ForeignKey(Event,on_delete=models.CASCADE)
name = models.CharField(max_length=255)
property_type = models.CharField(max_length=20, choices=TYPE_CHOICES)
expected_value = models.CharField(max_length=255)
class Meta:
verbose_name = 'Property'
verbose_name_plural = 'Properties'
def __str__(self):
return self.name
class TestForm(ModelForm):
class Meta:
model = Test
fields = ['name', 'description']
this should solve your problem if not let me know happy to help.
I have two models where employee have relation with person model but person have no relation with employee model.
Like:
class Person(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
class Employee(models.Model):
person = models.ForeignKey(Person, related_name='person_info')
code = models.CharField()
In such cases I want code field data in person serializer.
I can solved this with writing method in person model or using SerializerMethodField in person serializer
like this:
def get_employee_code(self):
return Employee.objects.get(person=self).id
and add this as source in person serializer
employee_code = serializers.CharField(source='get_employee_code')
Or adding employee serializer into person serialiszer
class PersonSerializer(serializers.ModelSerializer):
employee = EmployeeSerializer()
class Meta:
model = Person
fields = ('name', 'address', 'employee')
But i was trying to do this with reverse relation but i can't. I have tried like this, it gives an error
Serializer:
class PersonSerializer(serializers.ModelSerializer):
employee_code = serializers.CharField(source='person_info.code')
class Meta:
model = Person
fields = ('name', 'address', 'employee_code')
How can i solve this with reverse relation?
At the moment because you are using a ForeignKey field on the person attribute, it means that its returning a list when you access the reverse relation.
One solution would be to use a slug related field, though this must have many and read_only set to True, and will return a list because of the ForeignKey field.
class PersonSerializer(serializers.ModelSerializer):
employee_code = serializers.SlugRelatedField(
source='person_info',
slug_field='code',
many=True,
read_only=True,
)
class Meta:
model = Person
fields = ('name', 'address', 'employee_code')
The other option is to change your ForeignKey into a OneToOneField, which would still need read_only set to True but it will not return a list.
class Person(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
class Employee(models.Model):
person = models.OneToOneField(Person, related_name='person_info')
code = models.CharField()
class PersonSerializer(serializers.ModelSerializer):
employee_code = serializers.SlugRelatedField(
source='person_info',
slug_field='code',
read_only=True,
)
class Meta:
model = Person
fields = ('name', 'address', 'employee_code')
Or, if you don't want to change the ForeignKey, you could add a employee_code property method to the model instead to return the first employee code in the person_info relation.
class Person(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
#property
def employee_code(self):
employees = self.person_info.filter()
if employees.exists():
return employees.first().code
return ''
class Employee(models.Model):
person = models.OneToOneField(Person, related_name='person_info')
code = models.CharField()
class PersonSerializer(serializers.ModelSerializer):
employee_code = serializers.CharField(
read_only=True,
)
class Meta:
model = Person
fields = ('name', 'address', 'employee_code')
you can access the reverse relation with custom SerializerMethodField()
class PersonSerializer(serializers.ModelSerializer):
employee_code = serializers.SerializerMethodField()
def get_employee_code(self, obj):
return obj.person_info.code
class Meta:
model = Person
fields = ('name', 'address', 'employee_code')
When I make a call to my API (curl), I get the following:
{"id": 1, "name": "Sword", "persona": [1]}
I'm getting the 'id' of the persona, but not the properties within. How do I do this?
Models.py:
class Persona(models.Model):
name = models.CharField(max_length=255)
description = models.CharField(max_length=255)
icon = models.CharField(max_length=255,default='1')
def __str__(self):
return self.name
class Equipment(models.Model):
personas = models.ManyToManyField(Persona)
name = models.CharField(max_length=255)
def __str__(self):
return self.name
Serializers.py:
class EquipmentSerializer(serializers.ModelSerializer):
personas = serializers.PrimaryKeyRelatedField(queryset=Persona.objects.all(), many=True)
name = serializers.CharField(max_length=255)
class Meta:
model = Equipment
fields = ('id', 'name', 'persona')
You need two steps:
Create a Persona serializer class:
class PersonaSerializer(serializers.ModelSerializer):
class Meta:
model = Persona
Then you can "chain" this serializer to the Equipement one:
class EquipmentSerializer(serializers.ModelSerializer):
personas = PersonaSerializer(source='personas', many=True)
name = serializers.CharField(max_length=255)
class Meta:
model = Equipment
fields = ('id', 'name', 'personas')
For a deeper understanding of the above, have a look at this exemplary answer:
How do I include related model fields using Django Rest Framework?
Good luck :)