Saturday, August 10, 2019

Nested Model Issue

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

--
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+unsubscribe@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.

No comments:

Post a Comment