Show the views.py On Thu, 30 Mar 2023 at 4:36 AM, Amed Sheriff <amedsher...@gmail.com> wrote:
> 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 > <https://groups.google.com/d/msgid/django-rest-framework/CADteO3_BuN97kaGyiHU-b5VETJHLyxB4tD-ePn_FTuKEZsyKQg%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- *Abhishek Chauhan* ✆ : 9560 432 275 *@abhiazh* <https://twitter.com/abhiazh> -- 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/CAOaWsMGdZeV5qHzNhZuJLnMASBwq8dgg-CfqHi4yYj84Qr3UTg%40mail.gmail.com.