i'm trying to get nested object fields populated, however the only thing being returned is the primary key of each object (output below):
{
"name": "3037",
"description": "this is our first test product",
"components": [
1,
2,
3,
4
]
}
How do I have the component model's fields populated as well (and not just the PKs)? I would like to have the name and description included.
models.py
class Product(models.Model):
name = models.CharField('Bag name', max_length=64)
description = models.TextField ('Description of bag', max_length=512, blank=True)
urlKey = models.SlugField('URL Key', unique=True, max_length=64)
def __str__(self):
return self.name
class Component(models.Model):
name = models.CharField('Component name', max_length=64)
description = models.TextField('Component of product', max_length=512, blank=True)
fits = models.ForeignKey('Product', related_name='components')
def __str__(self):
return self.fits.name + "-" + self.name
serializers.py
from rest_framework import serializers
from app.models import Product, Component, Finish, Variant
class componentSerializer(serializers.ModelSerializer):
class Meta:
model = Component
fields = ('name', 'description', 'fits')
class productSerializer(serializers.ModelSerializer):
#components_that_fit = componentSerializer(many=True)
class Meta:
model = Product
fields = ('name', 'description', 'components')
#fields = ('name', 'description', 'components_that_fit' )
The documented approach doesn't seem to be working for me, and gives me the following error (you can see the lines following the standard approach commented out in the serializers.py entry above:
Got AttributeError when attempting to get a value for field 'components_that_fit' on serializer 'productSerializer'.
The serializer field might be named incorrectly and not match any attribute or key on the 'Product' instance.
Original exception text was: 'Product' object has no attribute 'components_that_fit'.
Update based on answer
Thanks to #Carlton's answer below, here's what is working for me:
serializers.py was changed and now looks like this:
class productSerializer(serializers.ModelSerializer):
components = componentSerializer(many=True)
class Meta:
model = Product
fields = ('name', 'description', 'components')
By calling the field components_that_fit, you're having the serialiser look for an attribute by that name. (There isn't one, hence your error.)
Two ways to fix it:
Call the field components, but declare it as components = componentSerializer(many=True)
Set source='components' field option when declaring the components_that_fit field.
The reason you get primary keys is that, unless declared explicitly, relations default to PrimaryKeyRelatedField.
I hope that helps.
Related
The problem is I have a 'details' field which should render into a nested relationship with it's parent serializer. I have tried a bunch of stuff and nothing seems to be working.
Here's my models:
class BusinessOrderModel(OrderToModel):
reference = models.IntegerField()
business_num = models.ForeignKey('BusinessModel', on_delete=models.CASCADE)
def __str__(self):
return str(self.reference)
class BusinessModel(models.Model):
Business_num = models.IntegerField(primary_key=True)
def __str__(self):
return str(self.Business_num)
class DetailModel(models.Model):
id = models.AutoField(primary_key=True)
detail = models.TextField()
order = models.ForeignKey('BusinessOrderModel', on_delete=models.CASCADE)
and here's my serializers which aren't working:
class DetailSerializer(serializers.ModelSerializer):
class Meta:
model = DetailModel
fields = ('id', 'detail')
class BusinessOrderSerializer(serializers.ModelSerializer):
details = DetailSerializer(many=True)
class Meta:
model = BusinessOrderModel
fields = ('reference', 'business_num', 'details')
I've tried many different things but I get this error:
Got AttributeError when attempting to get a value for field details
on serializer BusinessOrderSerializer. The serializer field might be
named incorrectly and not match any attribute or key on the
BusinessOrderModel instance. Original exception text was:
'BusinessOrderModel' object has no attribute 'details'.
Any help is much appreciated.
Thank you very much.
Using details to lookup reverse relationships only works if you set it as the related_name. The default for BusinessOrderModel to DetailModel will be detailmodel_set.
To make it accessible by calling details you should make this change:
class DetailModel(models.Model):
id = models.AutoField(primary_key=True)
detail = models.TextField()
order = models.ForeignKey('BusinessOrderModel', related_name="details", on_delete=models.CASCADE)
Now you can use DetailModel.objects.get(id=1).details.all()
You can also customize the query in your serializer:
class BusinessOrderSerializer(serializers.ModelSerializer):
details = SerializerMethodField()
class Meta:
model = BusinessOrderModel
fields = ('reference', 'business_num', 'details')
def get_details(self, obj):
return DetailSerializer(obj.details.filter(), many=True).data
I tried to check another topics, but didn't found a solution...
I have a many-to-many model, that have intermediate model with another field additional_field inside.
class BoardField(models.Model):
title = models.CharField(max_length=500, default='')
class Article(models.Model):
title = models.CharField(max_length=500, default='')
fields = models.ManyToManyField(BoardField, through='ArticleField', through_fields=('article', 'board_field'))
class ArticleField(models.Model):
article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='task')
board_field = models.ForeignKey(BoardField, on_delete=models.CASCADE)
additional_field = models.CharField(max_length=200, blank=True, null=True)
I want serialize Article with structure:
[
"title":"Title",
"fields":[
{
"board_field": {
"title":"Title"
},
"additional_field":"Additional info"
}
]
]
So, I wrote serializer:
class BoardFieldSrl(serializers.ModelSerializer):
class Meta:
model = BoardField
fields = (
'title',
)
class ArticleFieldSrl(serializers.ModelSerializer):
board_field = BoardFieldSrl()
class Meta:
model = ArticleField
fields = (
'board_field',
'additional_field',
)
class ArticleListSrl(serializers.ModelSerializer):
fields = ArticleFieldSrl(many=True)
class Meta:
model = Article
fields = (
'title',
'fields',
)
But I always got an error:
Got AttributeError when attempting to get a value for field `board_field` on serializer `ArticleFieldSrl`.
The serializer field might be named incorrectly and not match any attribute or key on the `BoardField` instance.
Original exception text was: 'BoardField' object has no attribute 'board_field'.
I made another several examples, but they doesn't gave my result, that I need... My maximum - I got BoardField with levels, but without intermediate model...
Can you help me with serializer, that return structure, that I mentioned above? It must include intermediate model ArticleField and nested BoardField.
Try fields = ArticleFieldSrl(source='articlefield_set', many=True)
You didn't specified a related_name at M2M field so the default naming is applied which is 'Intermediate model name'_set and if you want to use the fields on M2M relation you have to tell the serializer where to look for.
EDIT:
Camel removed from articlefield_set, model name is always converted to lower case
Using Django and its REST_Framework, I'm trying to add an object to my POSTGRES-11 database, whose model (Team) is related to another model (Country) by a ForeignKey relation. The JSON I send with the post request looks like this, where the number provided for country is supposed to be its id.
name:"test"
is_onsite:true
status:"PAID"
institution:"testU"
country:2
But then Django returns the following error:
IntegrityError at /api/register/team/
null value in column "country_id" violates not-null constraint
DETAIL: Failing row contains (28, , f, PENDING, , null).
I've tried sending the same json with 'country' replaced with 'country_id', but I've been faced with the same error.
I've already tried the solution given Here, but then Django returns this JSON instead of adding the object:
{
"country": [
"This field is required."
]
}
models.py:
class Country(models.Model):
name = models.CharField(max_length=255)
flag = models.ImageField()
class Meta:
verbose_name_plural = 'Countries'
def __str__(self):
return self.name
class Team(models.Model):
name = models.CharField(max_length=255, default="", blank=True)
is_onsite = models.BooleanField(default=False)
status = models.CharField(max_length=50, choices=TEAM_STATUS_CHOICES, default='PENDING')
institution = models.CharField(max_length=255, default="")
country = models.ForeignKey(Country, on_delete=models.CASCADE, default="", blank=True)
def __str__(self):
return self.name
serializers.py:
class CountrySerializer(serializers.ModelSerializer):
class Meta:
model = Country
fields = '__all__'
class TeamSerializer(serializers.ModelSerializer):
class Meta:
model = Team
fields = ['name', 'is_onsite', 'status', 'institution', 'country']
views.py:
class TeamCreateView(CreateAPIView):
queryset = Team.objects.all()
serializer_class = TeamSerializer
I should note that Django works perfectly fine when I try to add it via the HTML form provided at '/api/register/team', but not when it receives a JSON via outer post requests.
I've been stuck at this for days, so your help would be really appreciated.
#dirkgroten
I forgot to mention, I did the former thing by adding the following snippet to my TeamSerializer and tried sending the 'countryId' (and not 'country_id') in my POST request, but it still didn't work.
This snippet:
countryId = serializers.PrimaryKeyRelatedField(queryset=Country.objects.all())
def create(self, validated_data):
print(validated_data)
return Team.objects.create(
name=validated_data['name'],
is_onsite=validated_data['is_onsite'],
status=validated_data['status'],
institution=validated_data['institution'],
country=validated_data['countryId']
)
As for the latter case, I added the
country = CountrySerializer(many=False) line to my code, and sent a JSON like the following, but I still got the "This field is required" JSON.
My POST JSON:
name:"test"
is_onsite:true
status:"PAID"
institution:"testU"
country:{"name": "Test"}
Even though the country 'Test' exists in my database, it still fails to add the team.
First of all, you should remove default="", blank=True on your country field for the Team model. FK fields should never have empty string as default. If you want the possibly for your country to be "unset", use blank=True, null=True. But if every Team always must have a country, then don't add anything.
You can do two things:
Either you use 'country_id' in the fields of your TeamSerializer and pass the field as country_id in your JSON. You need to do both, passing country_id in your JSON but country as field in the serializer won't work.
fields = ['name', 'is_onsite', 'status', 'institution', 'country_id']
Or you add country = CountrySerializer(many=False) to your TeamSerializer but then in your json, you need to nest a country inside your request and it will create the country object:
{'name': 'test',
...
'country': {
'name': 'Angola'
}
}
I'm serialzing a Product model and its comments. Here's my simple code:
class ProductSerializer(serializers.HyperlinkedModelSerializer):
comment_set = CommentSerializer(many=True, read_only=True)
class Meta:
model = Product
fields = [
'title',
'comment_set'
]
class CommentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Comment
fields = [
'text',
]
class Comment(models.Model):
product = models.ForeignKey(Product, null=True, blank=True, db_index=True)
class Product(models.Model):
title = models.CharField(max_length=50)
...
Problem:
If the product has many comments. For example, 500 comments. All 500 of them got serialized.
How to limit the result to a number of my own choosing, like 100 comments?
I've done some research before posting this but only found questions about filtering.
Thank you.
Define a new method on the Product model that returns a query set with a limited number of comments.
Then pass that method as the source of the CommentSerializer inside your ProductSerializer.
class Product(models.Model):
title = models.CharField(max_length=50)
def less_comments(self):
return Comment.objects.all().filter(product=self).order_by("-id")[:100]
Then in the ProductSerializer:
class ProductSerializer(serializers.HyperlinkedModelSerializer):
comment_set = CommentSerializer(many=True, read_only=True, source="less_comments")
PS: Wrote the codes from memory, didn't test them. But should work.
You can write custom ListSerializer and put in CommentSerializer, then create custom field in ProductSerializer, wich source based on default related name:
class LimitedListSerializer(serializers.ListSerializer):
def to_representation(self, data):
data = data.all()[:100]
return super(FilteredListSerializer, self).to_representation(data)
class CommentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
list_serializer_class = LimitedListSerializer
model = Comment
fields = [
'text',]
class Product(serializers.HyperlinkedModelSerializer):
related_comments = CommentSerializer(many=True, read_only=True, source='comment_set')
when you pass many=True, list serrializer will be called.
You'll want to work on the CommentSerializer's queryset to control which ones you keep.
You'll be able to do that by overriding get_queryset. For example, to filter them against the current user. Note that I took this example because it highlights how to use the request's context to filter against:
class CommentSerializer(serializers.HyperlinkedModelSerializer):
def get_queryset(self):
user = self.context['request'].user
queryset = Comment.objects.filter(user=user)
return queryset
class Meta:
model = Comment
fields = [
'text',
]
I'm doing a HTTP PUT call to update the data of an object with a nested relationship, and I'm met by the following error:
HTTP 400 Bad Request
"AttributeChoice with this slug already exists."
The reason why this is confusing is because I'm doing a HTTP PUT call and I expect it to treat it as an UPDATE and not a CREATE.
My Models look like this:
class Attribute(models.Model):
name = models.CharField(max_length=100)
text_input = models.BooleanField(default=False)
slug = models.SlugField(unique=True)
class AttributeChoice(models.Model):
attribute = models.ForeignKey(Attribute)
value = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
My Serializers look like this:
class AttributeChoiceSerializer(serializers.ModelSerializer):
class Meta:
model = AttributeChoice
fields = '__all__'
extra_kwargs = {'id': {'read_only': False}}
class AttributeSerializer(serializers.ModelSerializer):
attributechoice_set = AttributeChoiceSerializer(many=True)
class Meta:
model = Attribute
fields = ('id', 'name', 'text_input', 'slug', 'attributechoice_set')
def update(self, instance, validated_data):
choice_data = validated_data.pop('attributechoice_set')
for choice in choice_data:
# If id is within the call, then update the object with matching id
if 'id' in choice:
try:
choice_obj = AttributeChoice.objects.get(pk=choice['id'])
choice_obj.value = choice['value']
choice_obj.slug = choice['slug']
choice_obj.attribute = instance
# If ID is not found, then create a new object
except AttributeChoice.DoesNotExist:
choice_obj = AttributeChoice(**choice)
# If no ID within the call, create a new object.
else:
choice_obj = AttributeChoice(**choice)
choice_obj.save()
return instance
Debug:
Even if I remove the update() function, I still get the same error. I believe the error is reported from when .is_valid() is called in the ViewSet. So it's not the update() that causes it.
Also, if I remove attributechoice_set = AttributeChoiceSerializer(many=True) and just include the attributechoice_set in the fields = (), the error disappears, but I need that line for the rest of the code to work.
Even through you're doing an update, it doesn't mean the nested data will just be updated.
You're simply saying that you want to update the top most object.
In some cases, you'll be removing or creating new nested objects while updating the top most one.
Therefore DRF considers by default that nested objects are for creation. You can work around this by explicitly removing the unique constraint on the nested serializer:
class AttributeChoiceSerializer(serializers.ModelSerializer):
class Meta:
model = AttributeChoice
fields = '__all__'
extra_kwargs = {
'id': {'read_only': False},
'slug': {'validators': []},
}
Someone has already developed a handy UniqueFieldsMixin for solving the problem:
pip install drf-writable-nested
now:
from drf_writable_nested import UniqueFieldsMixin
class User(models.Model):
name = models.CharField(max_length=200, unique=True)
class UserSerializer(UniqueFieldsMixin, serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'
I think it is because of the validators.
Like:
Django rest serializer Breaks when data exists
As my solution, I mark this nested field to read_only=True,
And do my own update, create function to access self.initial_data to handle myself.