Hi,
Please let me know how to write create methods in class based views. In X
table contains A, B, C, D foreign keys.example
Class X(models.Model) :
a=models.ForeignKey(A, on_delete=models.CASCADE)
B=fk
C=fk
D=fk


Thanks and regards.
Lakshman

On Sun, 3 Jan, 2021, 8:46 pm Carl Nobile, <carl.nob...@gmail.com> wrote:

> First, never use the PK that Django generates as a way to access a record
> through an API. These types of PKs are a numeric sequence and can leave
> your site open to sequence attacks where an attacker tries a long series of
> numbers to access all or many of your records. Use a public ID of some
> sort, you can use a UUID or generate your own complex ID.
> There should be no reason you should not be able to access a record with a
> company name. There is nothing in Django or DRF that would prevent that.
> You must have something in your code that is preventing it.
>
> ~Carl
>
> On Sun, Jan 3, 2021 at 2:35 AM Stats Student <stats.student4...@gmail.com>
> wrote:
>
>> Hi, I have a basic one-to-many foreign key relationship (each product
>> has a company associated with it, one company can have multiple
>> products). I do not specify an explicit serializer relation for this
>> field (e.g. PrimaryKeyRelatedField) so it's using the FK definition
>> from the models ( company = models.ForeignKey(Company, models.CASCADE,
>> blank=True, null=True) )
>>
>> I have overridden the .create() method in the Product serializer
>> (inherits from ModelSerializer) to use the company name that's passed
>> in, to look up the relevant ID (or create a new record) and assign it
>> to " validated_data['company'] = company_obj " and it works fine.
>>
>> However, on the update (PATCH) where I also pass in the company name,
>> I get a validation error saying that it expects a primary key instead
>> of the name ("Incorrect type. Expected pk value, received str"). I
>> have added breakpoints in .update() and .partial_update() of the
>> Product serializer but the error seems to come from an earlier
>> validation routine. Could someone suggest where I can intercept the
>> validation process to translate the company name into an object and
>> put in validated_data['company'] , the same way I am doing in
>> .create() ?
>>
>> I tried adding a field level validation, but that doesn't seem to make
>> a difference.
>>
>> def validate_company(self, value):
>>         c, created = Company.objects.get_or_create(name = value)
>>         return c
>>
>>
>> TIA
>>
>> --
>> 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/CAMZO7wJT7-OyjxRdubPKOAzZk15iGOdSw_stLaDra%3DyHipqPLg%40mail.gmail.com
>> .
>>
>
>
> --
>
> -------------------------------------------------------------------------------
> Carl J. Nobile (Software Engineer)
> carl.nob...@gmail.com
>
> -------------------------------------------------------------------------------
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Django REST framework" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-rest-framework/bAPSXq2RmY8/unsubscribe
> .
> To unsubscribe from this group and all its topics, 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/CAGQqDQL2LSgTFsOcWVmvAytwuJNRogtdRh%2B5ZrrMu%2BqgNbA8jg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-rest-framework/CAGQqDQL2LSgTFsOcWVmvAytwuJNRogtdRh%2B5ZrrMu%2BqgNbA8jg%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/CAKUoA6S9D0MKS4Zn577Zrh-uYCRuR%2ByyfK%3DQQjiJYwM2kB_06Q%40mail.gmail.com.

Reply via email to