创建具有外键的模型实例时的500代码

时间:2015-02-07 00:45:02

标签: python django

当我尝试创建ContactRequest模型对象时,我不确定为什么会出现ValueError。

输出:

(Pdb) unt
--Return--
> /Users/bli1/Development/Django/Boothie/home/views.py(80)load_home_content()->None
-> contact_request = ContactRequest(message=message, user=new_user.id)
(Pdb) l
 75                     phone_number = form.cleaned_data['phone_number']
 76                     new_user = User(first_name=first_name, last_name=last_name, phone_number=phone_number, email=email)
 77                     pdb.set_trace()
 78                     new_user.save()
 79                     message = form.cleaned_data['message']
 80  ->                 contact_request = ContactRequest(message=message, user=new_user.id)
 81                     if contact_request:
 82                         print "bad"
 83                     contact_request.save()
 84                     # pdb.set_trace()
 85                 return
(Pdb) unt
ValueError: ValueErr...tance.',)
> /Library/Python/2.7/site-packages/django/core/handlers/base.py(111)get_response()
-> response = wrapped_callback(request, *callback_args, **callback_kwargs)
(Pdb) l
106                             break
107     
108                 if response is None:
109                     wrapped_callback = self.make_view_atomic(callback)
110                     try:
111  ->                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
112                     except Exception as e:
113                         # If the view raised an exception, run it through exception
114                         # middleware, and if the exception middleware returns a
115                         # response, use that. Otherwise, reraise the exception.
116                         for middleware_method in self._exception_middleware:
(Pdb) 

view.py

def load_home_content(request):
    if request.method == "POST":
        print request.POST
        form = ContactForm(request.POST)
        print "form"
        print(form)
        # pdb.set_trace()
        if form.is_valid():
            # after is_valid(), the validated form data willbe in the form.cleaned_data dictionary.
            # Data will have been nicely converted into Python types
            email = form.cleaned_data['email']
            if User.objects.filter(email=email).exists():
                print("email exists")
                return render(request, 'home/home.html')
            else:
                print("email/user does not exist")
                first_name = form.cleaned_data['first_name']
                last_name = form.cleaned_data['last_name']
                phone_number = form.cleaned_data['phone_number']
                new_user = User(first_name=first_name, last_name=last_name, phone_number=phone_number, email=email)
                pdb.set_trace()
                new_user.save()
                message = form.cleaned_data['message']
                contact_request = ContactRequest(message=message, user=new_user.id)
                if contact_request:
                    print "bad"
                contact_request.save()
                # pdb.set_trace()
            return render(request, 'home/home.html')
        else:
            print "form invalid"
    return render(request, 'home/home.html', {'form': ContactForm})

models.py

class User(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    phone_number = models.CharField(max_length=20, blank=True)
    email = models.EmailField()

    def __str__(self):
        return self.first_name + " " + self.last_name

    class Meta:
        db_table = "home_users"


class ContactRequest(models.Model):
    user = models.ForeignKey(User)
    message = models.TextField(max_length=500)
    datetime_created = models.DateTimeField("Datetime created", auto_now_add=True)

    class Meta:
        db_table = "home_contactrequests"

1 个答案:

答案 0 :(得分:1)

您应该将User实例传递给ContactRequest构造函数:

contact_request = ContactRequest(message=message, user=new_user)