use*_*000 6 django django-models django-admin python-2.7
我想在dict模型中添加一个字段,dict在管理员上显示,并能够从管理员编辑它.
例如,我有一段感情
dict = { 'sister' : rel_score, 'mother' : rel_score, 'father': rel_score}
其中rel_score(默认值= 0)是每个关系的得分.我想将它存储dict到我的模型中并在admin中显示,以便我可以为管理员的每个关系分配这些rel_score.
此外,任何如何将分数(优先级)分配给不同元素,并根据这些分配的分数返回值的示例将非常有用.
实际上不可能有 DictField,因为底层数据库不支持这种类型的数据结构。
您可以有一个RelationshipScore 模型来存储您的关系,并将ForeignKey 从它存储到您现有的模型(我将使用用户作为示例)。例如:
Class RelationshipScore(models.Model):
user = models.ForeignKey(User)
relationship = models.CharField(max_length=50, primary_key=True)
score = models.IntegerField()
Run Code Online (Sandbox Code Playgroud)
然后,您可以向现有模型添加函数来获取值:
class User(models.Model):
def get_relationship(self, rel):
return RelationshipScore.objects.get(relationship=rel, user=self)
def get_lowest_relationship(self):
return RelationshipScore.objects.filter(user=self).order_by("-score")[0]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6678 次 |
| 最近记录: |