haven’t tried your code but can you like override

lookup_field = “userid” in the viewset class in views.py so that way your
view will look for the userid while making the post request .

But somehow after the lookup is done then the view will try to save to the
model using the serializers so there I am not very sure if it will work as
the field , userid, defined in your serializer Message_Counter_Serializer
.Try the lookup_field  first

On Thu, 30 Mar 2023 at 5:11 PM, Amed Sheriff <amedsher...@gmail.com> wrote:

> Hello Team
>
> Please this is the view requested.
>
> class Counter_Viewsets(errorhandler, viewsets.ModelViewSet):
>     permission_classes = [permissions.IsAuthenticated,
> TokenHasResourceScope]
>     serializer_class = Message_Counter_Serializer
>
>     def get_queryset(self):
>         return
> Counter.objects.all().filter(userid__user_name=self.request.user)
>
> Thanks and very much appreciated your support
>
> Best regards
>
>
> Amed.
>
>
> On Thu, Mar 30, 2023 at 3:13 AM Abhishek Chauhan <abk...@gmail.com> wrote:
>
>> 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
>> <https://groups.google.com/d/msgid/django-rest-framework/CAOaWsMGdZeV5qHzNhZuJLnMASBwq8dgg-CfqHi4yYj84Qr3UTg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> 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_OuOeFn%3DGwXLaOssQ9HZnbra5ht_knBKpuJUon4ySzSQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-rest-framework/CADteO3_OuOeFn%3DGwXLaOssQ9HZnbra5ht_knBKpuJUon4ySzSQ%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/CAOaWsMHR9fFcdcm74--2MXvBs0vvCPK_45g5m76CW4Vny2FkUQ%40mail.gmail.com.

Reply via email to