在我的 Django 项目中,假设我有以下模型:(
class City(models.Model):
name = models.CharField(max_length=255)
freebase_id = models.CharField(max_length=255)
latitude = models.DecimalField()
longitude = models.DecimalField()
area = models.IntegerField()
为了简单起见,我省略了 DecimalField 所需的参数)。
用户可以手动输入所有字段,但我希望通过让用户在管理区域中输入某种 ID(例如 Freebase ID)来让生活变得更轻松(例如 /en/manchester_united_kingdom),这样我们就可以使用 API 来获取诸如 latitude
、经度
和面积
。
长话短说,我想让用户提供一些 ID,然后可以使用它来派生模型内的其他数据。在理想的情况下,派生字段最初应该隐藏在管理系统中,但一旦填充后就变得可见,以便可以对其进行编辑。
这样的事可能吗?
In my Django project, let's say I have the following model:
class City(models.Model):
name = models.CharField(max_length=255)
freebase_id = models.CharField(max_length=255)
latitude = models.DecimalField()
longitude = models.DecimalField()
area = models.IntegerField()
(I've omitted the required parameters for DecimalField for simplicity).
The user could enter all the fields manually, but I want to make life easier by letting the user enter some kind of ID in the admin area, like a Freebase ID (such as /en/manchester_united_kingdom), so that we can then use the API to fetch things like latitude
, longitude
and area
.
Long story short, I want to let users provide some ID which can then be used to derive other data inside the model. In an ideal world, the derived fields should be initially hidden in the admin system, but then made visible once populated so that they can be edited.
Is such a thing possible?
发布评论
评论(2)
我正在我的 UserProfile 模型中做类似的事情。我有一个邮政编码字段,如果用户填写了该字段,则用于进行地理查找以获取城市/州和纬度/经度并将它们存储在模型中各自的字段中:
请注意,派生表单的 < code>zip 字段是唯一对用户可见的字段。
I am doing something similar in my
UserProfile
model. I have a zip code field that, if it gets filled in by the user, is used to do a geo lookup to get city/state and lat/lng and store them in their respective fields in the model:Note that the derived form's
zip
field is the only one visible to the user.如果您重写对象的 save() 方法,您可以在其中设置经纬度。但现在不确定是否以这种方式隐藏字段。最新的 Django 中有针对只读字段的新内容,如果您可以通过自定义管理管理器神奇地做到这一点,您可能有办法......
If you override the save() method of your object you can set your lat-long in that. Not sure now to hide fields in that way though. There's new stuff in the latest Django for read-only fields, and if you can do that magically via a custom admin manager you might have a way in...