I am attempting to auto-populate the 'pub_date' field in my BucketListItemForm. I have been able to do this with the time and date field but continually get the following error when trying to do this with the 'pub_by' field:
no such column: BucketList_bucketlistitem.pub_by_id
I have been playing around with everything I can think of and looking at other people with similar problems posted on StackOverflow over the past few days but nothing seems to be solving my problem. I would greatly appreciate any amount of help you could offer!
forms.py
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)
admin.py
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 %}
create_item.html
<form action = "/bucketlist/create/" method = "post">{% csrf_token %}
<ul>
{{form.as_ul}}
</ul>
<input type = "submit" name = "submit" value = "Create BucketListItem">
</form>
It looks like you forgot to sync the database after updating your code. Try deleting the database you're using (or dropping it) and run syncdb
again. I could help you further if you post your complete code ie html files with the forms rendered if this does not work.