I'm trying to save 100 characters form user in a 'microblog' minimal application. My code seems to not have any mystakes, but doesn't work. The mistake is in views.py, I can't save the foreign key to user table.
models.py
looks like this:
class NewManager(models.Manager):
def create_post(self, post, username):
new = self.model(post=post, created_by=username)
new.save()
return new
class New(models.Model):
post = models.CharField(max_length=120)
date = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, blank=True)
objects = NewManager()
class NewForm(ModelForm):
class Meta:
model = New
fields = ['post']
# widgets = {'post': Textarea(attrs={'cols': 80, 'rows': 20})
def save_new(request):
if request.method == 'POST':
created_by = User.objects.get(created_by = user)
date = request.POST.get('date', '')
post = request.POST.get('post', '')
new_obj = New(post=post, date=date, created_by=created_by)
new_obj.save()
return HttpResponseRedirect('/')
else:
form = NewForm()
return render_to_response('news/new_form.html', {'form': form},context_instance=RequestContext(request))
I didn't mention imports here - they're done right, anyway. My mistake is in views.py. When I try to save it says:
local variable 'created_by' referenced before assignment
If I put created_py
as a parameter, the save needs more parameters. It is really weird.