Nested Model Issue

21 views
Skip to first unread message

Soumen Khatua

unread,
Aug 10, 2019, 2:55:22 PM8/10/19
to django...@googlegroups.com
Hi Folks,
My requirement is whenever i create a user in post method that user can pass username and password, Address models contain some field and Profile models contains some field. If he is authenticated users and want to create one prifile he need to pass Address model contains some field and Profile models contain some fields information. I'm stuckung in this problem from last four days. Please help me with complete code actually I'm not that much good in Django rest framework. please help me guys.

These are my models for your reference:


models.py:
---------------

from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Address(models.Model):
    street_no = models.CharField(max_length = 65)
    city = models.CharField(max_length = 65)
    state = models.CharField(max_length = 65)
    pincode = models.CharField(max_length = 65)
    country = models.CharField(max_length = 65)
    created_at = models.DateField(auto_now_add = True)
    updated_at = models.DateField(auto_now = True)

    def __str__(self):
        return self.city


class Profile(models.Model):
    GENDER_CHOICES = (
      ('male', 'Male'),
      ('female', 'Female')
    )
    user = models.ForeignKey(User,on_delete=models.CASCADE,related_name = 'usermodel')
    permanent_address_city = models.ForeignKey(Address,on_delete=models.CASCADE,related_name = 'permanent_address_city')
    phone_number = models.PositiveIntegerField()
    gender = models.CharField(max_length = 5,choices = GENDER_CHOICES)
    profile_pic = models.ImageField(upload_to='profile_pic',default = 'default.jpg')
    created_at = models.DateField(auto_now_add = True)
    updated_at = models.DateField(auto_now = True)



Thank you in advance

mohamed habib

unread,
Aug 10, 2019, 5:39:18 PM8/10/19
to django...@googlegroups.com
You can use a signal to ensure that a profile is created for each user. Make sure all the fields in your Profile model are nullable:

```
class Profile(models.Model):
    GENDER_CHOICES = (
      ('male', 'Male'),
      ('female', 'Female')
    )
    user = models.ForeignKey(User,on_delete=models.CASCADE,related_name = 'usermodel')
    permanent_address_city = models.ForeignKey(Address,on_delete=models.CASCADE,related_name = 'permanent_address_city', null=True, blank=True)
    phone_number = models.PositiveIntegerField(, null=True, blank=True)
    gender = models.CharField(max_length = 5,choices = GENDER_CHOICES, null=True, blank=True)
    profile_pic = models.ImageField(upload_to='profile_pic',default = 'default.jpg', null=True, blank=True)
    created_at = models.DateField(auto_now_add = True, null=True, blank=True)
    updated_at = models.DateField(auto_now = True, null=True, blank=True)
```
use signal within your models.py to create a blank profile every time a User object is created:

```
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)
```
Now you can create an Update profile view to update the user based on the fields he has placed in the instance using your serialiser. You can have a separate view to create an address object. The User Profile update view can take the ID of an address and attach that address to the user.


--
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 django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WZCT_fB-bgjWmLTiPDG81V5Vg-BPqeeps_QivtpddrkaQ%40mail.gmail.com.


--
Best regards,
Mohammed M. Habib, PhD

Soumen Khatua

unread,
Aug 12, 2019, 12:05:26 AM8/12/19
to django...@googlegroups.com
Actually my query is different I want to do Get, Post and Update(Crud) operation by using rest framework on that following models. 
Thanks for your response. 

Reply all
Reply to author
Forward
0 new messages