我想知道將這兩個模型與其他模型(如 Order、OrderItem 等)一起使用的可能性。
我嘗試像在我的 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)
有可能有像上面這樣的模型嗎?
如果是,
如何在 views.py 中顯示它以驗證用戶(組態檔)并同時創建客戶。
uj5u.com熱心網友回復:
我認為您的上述模型結構可能運行良好,但在 views.py 中您可以使用令牌身份驗證,并且在創建客戶時您可以驗證用戶是否存在。
更多更多您可以user = request.user在您的視圖中使用,然后
profile = Profile.objects.get(user__id=user)
if profile:
.............
create your customer here
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/412406.html
標籤:
