Python 在Django中自动填充用户字段(无此类列)
我正在尝试自动填充BucketListItemForm中的“发布日期”字段。我已经能够使用“时间和日期”字段执行此操作,但在尝试使用“发布者”字段执行此操作时,不断出现以下错误: 没有这样的列:BucketList_bucketlistitem.pub_by_id 在过去的几天里,我一直在玩弄我能想到的一切,看着StackOverflow上有类似问题的其他人,但似乎没有什么能解决我的问题。如果您能提供任何帮助,我将不胜感激 forms.pyPython 在Django中自动填充用户字段(无此类列),python,django,forms,admin,auto-populate,Python,Django,Forms,Admin,Auto Populate,我正在尝试自动填充BucketListItemForm中的“发布日期”字段。我已经能够使用“时间和日期”字段执行此操作,但在尝试使用“发布者”字段执行此操作时,不断出现以下错误: 没有这样的列:BucketList_bucketlistitem.pub_by_id 在过去的几天里,我一直在玩弄我能想到的一切,看着StackOverflow上有类似问题的其他人,但似乎没有什么能解决我的问题。如果您能提供任何帮助,我将不胜感激 forms.py class BucketListItemForm(fo
class BucketListItemForm(forms.ModelForm):
class Meta:
model = BucketListItem
models.py
class BucketListItem(models.Model):
text = models.CharField(max_length = 200)
pub_by = models.ForeignKey(User)
pub_date = models.DateTimeField(editable=False)
def __unicode__(self):
return self.text
def recently_added(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days = 1)
recently_added.boolean = True
def save(self):
if not self.id:
self.pub_date = timezone.now()
super(BucketListItem, self).save()
views.py
def index(request):
all_list_items = BucketListItem.objects.all()
context = {'all_list_items': all_list_items}
return render(request, 'BucketList/index.html', context)
def my_list(request):
personal_list = BucketListItem.objects.filter(pub_by = request.user.username)
context = {'personal_list': personal_list,
'user': request.user.username}
return render(request, 'BucketList/mylist.html', context)
def create(request):
if request.POST:
form = BucketListItemForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/bucketlist/mylist')
else:
form = BucketListItemForm()
args = {}
args.update(csrf(request))
args['form'] = form
return render_to_response('BucketList/create_item.html', args)
管理员
class BucketListItemAdmin(admin.ModelAdmin):
fieldsets = [
('Text', {'fields': ['text']}),
]
readonly_fields = ('pub_date',)
list_display = ['text', 'pub_date', 'pub_by', 'recently_added']
list_filter = ['pub_date', 'pub_by']
search_fields = ['text']
def save_model(self, request, obj, form, change):
if not change:
obj.user = request.user
super(self.__class__, self).save_model(request, obj, form, change)
admin.site.register(BucketListItem, BucketListItemAdmin)
mylist.html
{% if user %}
<h1>{{user}}'s Bucket List</h1>
{% if personal_list %}
<ol>
{% for item in personal_list %}
<li>{{ item.text }}</li>
{% endfor %}
</ol>
{% else %}
<p>You don't have any Bucket List items yet! Better get on that.</p>
{% endif %}
{% else %}
<p>Sorry, it doesn't seem like you are logged in!</p>
{% endif %}
创建_item.html
<form action = "/bucketlist/create/" method = "post">{% csrf_token %}
<ul>
{{form.as_ul}}
</ul>
<input type = "submit" name = "submit" value = "Create BucketListItem">
</form>
更新代码后,您似乎忘记同步数据库。尝试删除或删除正在使用的数据库,然后再次运行syncdb。如果您发布完整的代码ie html文件和表单,我可以进一步帮助您,如果这不起作用。非常感谢您的回答!我听从了你的建议,现在一切正常,除了上面错误消息中所述的mylist视图:int的文本无效,以10为基数:“Mike”,其中“Mike”是我当前登录的帐户,“create”视图为“pub_by”字段生成下拉列表,而不是自动将用户名传递给表格。如果您对此有任何建议,我将不胜感激!NP很高兴我能帮忙。要实现这一点,您必须更改表单定义。您还需要将用户传递到表单,或在保存表单时将其排除并添加。另一个是python错误:您试图从没有中间值的字符串中传递int。例如,int'word'I已经解决了所有问题!非常感谢你的帮助。