Home > database >  Is the any possibility of having a Profile model and Customer model in Django e-commerce development
Is the any possibility of having a Profile model and Customer model in Django e-commerce development

Time:01-17

I wish to know the possibility of having those two models altogether with other models like Order, OrderItem etc.

I tried running it like in my models.py

class Profile(models.Model):
    user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
    name = models.CharField(max_length=30, null=True, blank=True, help_text="Insert your name")
    image = models.ImageField(default='default.png', upload_to='profile_pics')

class Customer(models.Model):
    name = models.OneToOneField(Profile, on_delete=models.CASCADE)
    phone = models.CharField(max_length=11, null=True, blank=True)
    address = models.CharField(max_length=20, null=True, blank=True)
    phone = models.CharField(max_length=11, null=True, blank=True)
    address = models.CharField(max_length=20, null=True, blank=True)

class Order(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True, blank=True)
    complete = models.BooleanField(default=False, null=True, blank=True)
    date_ordered = models.DateTimeField(auto_now_add=True)
    transaction_id = models.CharField(max_length=100, null=True)

Is it possible having such models like the above?

If Yes,

How can I present it in the views.py to authenticate a User(Profile) and at the same create a Customer.

CodePudding user response:

I think Your above Model Structure may work well, but in views.py you can use token authentication and at the time of customer creation you can authenticate if user is exist or not.

Further More You can use user = request.user in your views and then

profile = Profile.objects.get(user__id=user)
if profile:
 .............
 create your customer here
  •  Tags:  
  • Related