[英]I have a OneToOne relationship between two objects of the same class in a Django app. Is it possible to enforce the uniqueness of this relationship?
[英]How do i have OneToOne relationship between any two fields of different models in Django?
class HmsMedicine(models.Model):
id = models.IntegerField(primary_key=True)
medicine_name = models.CharField(max_length=100)
price = models.IntegerField(max_length=6)
class HmsBilling(models.Model):
id = models.IntegerField(primary_key=True)
regid = models.ForeignKey(HmsPatient, db_column='regid')
medicine = models.ForeignKey(HmsMedicine, db_column='medicine')
quantity = models.IntegerField()
rate = models.IntegerField()
我想在rate
字段中輸入price
字段的值。
class HmsBilling(models.Model):
id = models.IntegerField(primary_key=True)
regid = models.ForeignKey(HmsPatient, db_column='regid')
medicine = models.ForeignKey(HmsMedicine, db_column='medicine')
quantity = models.IntegerField()
rate = models.ForeignKey(HmsMedicine, db_column='price')
hms = HmsBilling.objects.select_related().get(id=5)
hms.rate
這將為您提供ID = 5的結算價格(有關選擇相關內容,請參閱此鏈接以獲取參考)
更好:
class HmsBilling(models.Model):
id = models.IntegerField(primary_key=True)
medicine = models.ForeignKey(HmsMedicine)
quantity = models.IntegerField()
hms = HmsBilling.objects.select_related().get(id=5)
hms.medicine.price
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.