我有一個訂單模式:Django的:限制models.ForeignKey結果
class Order(models.Model):
profile = models.ForeignKey(Profile, null=True, blank=True)
它返回的所有可能的配置文件的命令,這是沒有必要的,因此減慢了管理秩序網頁的加載速度。
我希望簡單地返回簡單地作爲下訂單的用戶的配置文件。我試着將其更改爲:
class Order(models.Model):
profile = models.ForeignKey(Profile, null=True, blank=True, limit_choices_to={'order': 99999})
它返回訂單號99999正確的配置文件,但我怎麼能得到這個動態。 Order模型不知道「self」,但訂單號包含在URL中。
這樣做的最好方法是什麼?
如果配置文件是固定的,它聽起來好像根本不應該可編輯。您可以將其設爲[只讀字段](https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields),並將值設置爲[當訂單被放置](https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model)。 – Alasdair
您使用的是django管理員嗎? –
是的,這是與django管理站點 – James