Hello Team,

Please i need help with this which has taken me over two weeks and still
couldn't come up with anything after surfing the internet.

Is there any posibility to post data to foreign key using the value rather
than the FK ID. Bcz i have tried all kinds of scenarios but none seemed to
work with this even though they do refer to  "SlugRelatedField" usage.

This is my model.

class Counter(models.Model):
    userid = models.ForeignKey(Identification,related_name="userName",
 verbose_name=_("User ID"), on_delete=models.CASCADE, null=True)
    counter = models.IntegerField(_("Counter"), blank=True, null=True)
    date_and_time = models.DateTimeField(_("Date and Time"),
default=timezone.now)

    def __str__(self):
        return str(self.counter)

This is my serializer.

class Message_Counter_Serializer(serializers.ModelSerializer):
    userid = serializers.SlugRelatedField(
        slug_field='userName',
        queryset=Identification.objects.all()
    )

    class Meta:
        model = Counter
        fields = '__all__'

Post:
{
"counter": "2",
"userid": "ya"
}

Error:
 {"userid":["Invalid value."]}

Thanks in advance

Best regards

Amed.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/CADteO3_BuN97kaGyiHU-b5VETJHLyxB4tD-ePn_FTuKEZsyKQg%40mail.gmail.com.

Reply via email to