Hi,

This is possible if you have a nested serializer that allow creation. We
have created a plugin for that :
https://pypi.org/project/drf-nested/

There are several others out there that do this also - so it's completely
possible.

However when looking at your code  - I think the relationship isn't
correct? The way you have written your code - the image is the main object
and the company is secondary to it. So when you delete the image, the
company will also be deleted - and you can add several companies to the
same image, and not the other way around. If that is the way you want it
then it's fine. But I would have a foreign key in the image to the company.

Regards,

Andréas


Den ons 28 aug. 2019 kl 12:37 skrev Soumen Khatua <[email protected]
>:

> Hi Folks,
>  I have three models like Company,Employee and images. Images is
> foreignkey in my Company table. At the time of creating Company deatils I
> want to add image also, How I add these image in my image tables and at the
> time to get method how i can show company details along with image using
> django rest framework.
>
> *models.py*
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *class Image(models.Model):    company_image =
>  models.ImageField(upload_to='company_image',default = 'default.jpg')
> created_at = models.DateField(auto_now_add = True)    updated_at =
> models.DateField(auto_now = True)    class Meta:        verbose_name =
> 'image'    def  __str__(self):        return self.company_imageclass
> Company(models.Model):    cmp_image = models.ForeignKey(Image,related_name
> = 'comp_image',on_delete =
>                                   models.CASCADE)    name =
> models.CharField(max_length = 100)    reg_no = models.IntegerField()    ceo
> = models.CharField(max_length = 100)    location =
> models.CharField(max_length = 100)    active =
> models.BooleanField(default=True)    created_at =
> models.DateField(auto_now_add = True)    updated_at =
> models.DateField(auto_now = True)    def __str__(self):        return
> self.name <http://self.name>*
>
>
> Thank You
>
> Regards,
> Soumen
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAPUw6WYjj4ByDwcKWhdnwCU0039WW0frfG0qF715HqAONgi1eg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAPUw6WYjj4ByDwcKWhdnwCU0039WW0frfG0qF715HqAONgi1eg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK4qSCc4Eoh9Hw%2BahTsyPdUhPbb_Lqx7dTJWcBA0uA4itFVKbQ%40mail.gmail.com.

Reply via email to