无法在auto_now = True的表单上显示DateField [英] Can't display DateField on form with auto_now = True
问题描述
我有一个带有auto_now的模型,auto_now_add设置为更新和创建字段:
I have a model with auto_now, and auto_now_add set for Update and Create fields:
class HotelProfiles(models.Model):
fe_result_id = models.AutoField(primary_key=True)
fe_created_date = models.DateTimeField(verbose_name='Created',
blank=True,
auto_now_add=True)
fe_updated_date = models.DateTimeField(verbose_name='Updated',
blank=True,
auto_now=True)
在管理员中,它显示两个字段,但不显示它们。他们的
似乎没有被传递给我的表单来渲染。我不希望他们
可以编辑,但我想显示在我的表格的顶部。
我该怎么做?
In the Admin it displays both fields but leaves them uneditable. They don't seem to be passed to my form to be rendered. I don't want them to be editable, but I would like to display at the top of my form. How can I do this?
这是在我的HotelProfilesAdmin类中:
This is in my HotelProfilesAdmin class:
readonly_fields = ('fe_result_id', 'fe_created_date', 'fe_updated_date', 'fe_owner_uid')
#date_hierarchy = 'lto_end_date'
fieldsets = (
("Internal Use Only", {
'classes': ('collapse',),
'fields': ('fe_result_id', 'fe_created_date', 'fe_owner_uid', 'fe_updated_date', 'fe_result_status')
}),
推荐答案
为了别人的利益,我想出了一个这样做的方法,我是Django的新手,所以如果有一个更好的方法,我有兴趣听到它,视图代码如下,我不知道Django是否没有返回查询中的字段,我发现它是这样的,所以我不明白的表单中的某些东西,删除了这些字段,所以不能被渲染,所以我把它复制到一个dict在渲染之前调用read_only并将其传递。
For the benefit of others, I figured out a way to do this. I'm new to Django, so if there is a better way, I'd be interested in hearing it. The view code is below. I wasn't sure if Django was not returning the fields from the query, and I found out that it was. So, something in the renderering of the form that I don't understand removed those fields so they couldn't be rendered. So, I copied them to a dict called read_only before rendering and passed it along.
try:
hotel_profile = HotelProfiles.objects.get(pk=hotel_id)
read_only["created_on"] = hotel_profile.fe_created_date
read_only["updated_on"] = hotel_profile.fe_updated_date
f = HotelProfileForm(instance=hotel_profile)
#f.save()
except:
f = HotelProfileForm()
print 'rendering blank form'
return render_to_response('hotels/hotelprofile_form.html', {'f' : f, 'read_only': read_only}, context_instance=RequestContext(request))
这篇关于无法在auto_now = True的表单上显示DateField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!