Discussion:
Losing foreign key value in Django REST serializer
Alexander Teut
2018-05-29 15:13:11 UTC
Permalink
0down votefavorite
<https://stackoverflow.com/questions/50587735/losing-foreign-key-value-in-django-rest-serializer#>

I've simplified my code to make problem more clear.

I have two related models in my Django REST project. In my models.py:

class Project(Models.model):
name = models.CharField(max_length=150)
class Item(models.Model):
project = models.ForeignKey(Project, blank=True, on_delete=models.CASCADE)
data = models.TextField(blank=True, null=True)

Then I create a serialiser:

class ItemSerializer(serializers.ModelSerializer):
class Meta:
model = Item
fields = ('id', 'project_id', "data")

View set:

class ItemViewSet(viewsets.ModelViewSet):
queryset = Item.objects.all()
serializer_class = ItemSerializer
filter_backends = (DjangoFilterBackend,)
filter_fields = ('project_id')

Router:

router.register(r'items', ItemViewSet)

This implementation works fine for models without foreign keys. But when I
post something like:

{
project_id: 1,
data: "text"
}

(the project with id=1 exists)

my ItemSerializer gets just { data: "text" } to validated_fields.

The project_id field is *completely lost* during validation.

How to configure Serializer or ViewSet to make it workable?
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Xavier Ordoquy
2018-05-29 16:07:18 UTC
Permalink
Hi,

In your serializer and your payload, replace « project_id » with « project ».
DRF expects same named relation fields to be serialized/deserialized by default.

project_id exists in the model so it’s accepted by DRF but since it has no field in the model associated (it’s a property) it’ll be a ReadOnlyField.

Regards,
Xavier Ordoquy,
Linovia
Post by Alexander Teut
0
down vote
<>favorite
<https://stackoverflow.com/questions/50587735/losing-foreign-key-value-in-django-rest-serializer#>
I've simplified my code to make problem more clear.
name = models.CharField(max_length=150)
project = models.ForeignKey(Project, blank=True, on_delete=models.CASCADE)
data = models.TextField(blank=True, null=True)
model = Item
fields = ('id', 'project_id', "data")
queryset = Item.objects.all()
serializer_class = ItemSerializer
filter_backends = (DjangoFilterBackend,)
filter_fields = ('project_id')
router.register(r'items', ItemViewSet)
{
project_id: 1,
data: "text"
}
(the project with id=1 exists)
my ItemSerializer gets just { data: "text" } to validated_fields.
The project_id field is completely lost during validation.
How to configure Serializer or ViewSet to make it workable?
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alexander Teut
2018-05-30 08:32:19 UTC
Permalink
Thank you!

I fixed it this way:

class ItemSerializer(serializers.ModelSerializer):

research = serializers.PrimaryKeyRelatedField(queryset=Project.objects.all())

class Meta:
model = Item
fields = ('id', 'project', "data")
Post by Xavier Ordoquy
Hi,
In your serializer and your payload, replace « project_id » with « project ».
DRF expects same named relation fields to be serialized/deserialized by default.
project_id exists in the model so it’s accepted by DRF but since it has no
field in the model associated (it’s a property) it’ll be a ReadOnlyField.
Regards,
Xavier Ordoquy,
Linovia
0down votefavorite
<https://stackoverflow.com/questions/50587735/losing-foreign-key-value-in-django-rest-serializer#>
I've simplified my code to make problem more clear.
name = models.CharField(max_length=150)
project = models.ForeignKey(Project, blank=True, on_delete=models.CASCADE)
data = models.TextField(blank=True, null=True)
model = Item
fields = ('id', 'project_id', "data")
queryset = Item.objects.all()
serializer_class = ItemSerializer
filter_backends = (DjangoFilterBackend,)
filter_fields = ('project_id')
router.register(r'items', ItemViewSet)
This implementation works fine for models without foreign keys. But when I
{
project_id: 1,
data: "text"
}
(the project with id=1 exists)
my ItemSerializer gets just { data: "text" } to validated_fields.
The project_id field is *completely lost* during validation.
How to configure Serializer or ViewSet to make it workable?
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Pallavi Singh
2018-05-30 18:59:23 UTC
Permalink
i am new for drf,and i m trying to do forget password ,how can i do?
Post by Alexander Teut
Thank you!
research = serializers.PrimaryKeyRelatedField(queryset=Project.objects.all())
model = Item
fields = ('id', 'project', "data")
втПрМОк, 29 Ќая 2018 г., 19:07:23 UTC+3 пПльзПватель Xavier Ordoquy
Post by Xavier Ordoquy
Hi,
In your serializer and your payload, replace « project_id » with « project ».
DRF expects same named relation fields to be serialized/deserialized by default.
project_id exists in the model so it’s accepted by DRF but since it has
no field in the model associated (it’s a property) it’ll be a ReadOnlyField.
Regards,
Xavier Ordoquy,
Linovia
0down votefavorite
<https://stackoverflow.com/questions/50587735/losing-foreign-key-value-in-django-rest-serializer#>
I've simplified my code to make problem more clear.
name = models.CharField(max_length=150)
project = models.ForeignKey(Project, blank=True, on_delete=models.CASCADE)
data = models.TextField(blank=True, null=True)
model = Item
fields = ('id', 'project_id', "data")
queryset = Item.objects.all()
serializer_class = ItemSerializer
filter_backends = (DjangoFilterBackend,)
filter_fields = ('project_id')
router.register(r'items', ItemViewSet)
This implementation works fine for models without foreign keys. But when
{
project_id: 1,
data: "text"
}
(the project with id=1 exists)
my ItemSerializer gets just { data: "text" } to validated_fields.
The project_id field is *completely lost* during validation.
How to configure Serializer or ViewSet to make it workable?
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Francisco Pandol
2018-05-31 12:55:39 UTC
Permalink
Pallavi you can use https://github.com/sunscrapers/djoser
But the next time please create a new thread for an unrelated question.
Post by Pallavi Singh
i am new for drf,and i m trying to do forget password ,how can i do?
On Wed, May 30, 2018 at 2:02 PM, Alexander Teut <
Post by Alexander Teut
Thank you!
research = serializers.PrimaryKeyRelatedField(queryset=Project.objects.all())
model = Item
fields = ('id', 'project', "data")
втПрМОк, 29 Ќая 2018 г., 19:07:23 UTC+3 пПльзПватель Xavier Ordoquy
Post by Xavier Ordoquy
Hi,
In your serializer and your payload, replace « project_id » with
« project ».
DRF expects same named relation fields to be serialized/deserialized by default.
project_id exists in the model so it’s accepted by DRF but since it has
no field in the model associated (it’s a property) it’ll be a ReadOnlyField.
Regards,
Xavier Ordoquy,
Linovia
0down votefavorite
<https://stackoverflow.com/questions/50587735/losing-foreign-key-value-in-django-rest-serializer#>
I've simplified my code to make problem more clear.
name = models.CharField(max_length=150)
project = models.ForeignKey(Project, blank=True, on_delete=models.CASCADE)
data = models.TextField(blank=True, null=True)
model = Item
fields = ('id', 'project_id', "data")
queryset = Item.objects.all()
serializer_class = ItemSerializer
filter_backends = (DjangoFilterBackend,)
filter_fields = ('project_id')
router.register(r'items', ItemViewSet)
This implementation works fine for models without foreign keys. But when
{
project_id: 1,
data: "text"
}
(the project with id=1 exists)
my ItemSerializer gets just { data: "text" } to validated_fields.
The project_id field is *completely lost* during validation.
How to configure Serializer or ViewSet to make it workable?
--
You received this message because you are subscribed to the Google
Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Francisco Pandol
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...