IntegrityError:“ user_id”列中的空值违反了非空约束

时间:2019-11-24 04:27:35

标签: django-testing

我正在django中进行测试,但是我一直收到这个完整性错误,我不知道该怎么办。有人可以帮忙吗?

models.py

  class Home(models.Model): 
      agent = models.ForeignKey(Agent, on_delete=models.DO_NOTHING, blank=True, null=True)
      user = models.ForeignKey(User, on_delete=models.CASCADE, related_name=None)
      property_title = models.CharField(max_length=150)
      address = models.CharField(max_length=150)
      city = models.CharField(max_length=200)

      state = models.CharField(max_length=100)
      zipcode = models.CharField(max_length=100)
      information = models.TextField(default='')
      price = models.IntegerField()
      bedrooms = models.IntegerField()
      bathrooms = models.DecimalField(max_digits=2, decimal_places=1)
      garage = models.IntegerField(default=0)
      homesize = models.IntegerField()
      landsize = models.DecimalField(max_digits=5, decimal_places=1)
      top_photo = models.FileField(upload_to='pictures/%Y/%m/%d/')
      picture1 = models.FileField(upload_to='pictures/%Y/%m/%d/', blank=True)
      picture2 = models.FileField(upload_to='pictures/%Y/%m/%d/', blank=True)
      picture3 = models.FileField(upload_to='pictures/%Y/%m/%d/', blank=True)
      picture4 = models.FileField(upload_to='pictures/%Y/%m/%d/', blank=True)
      picture5 = models.FileField(upload_to='pictures/%Y/%m/%d/', blank=True)
      picture6 = models.FileField(upload_to='pictures/%Y/%m/%d/', blank=True)
      is_posted = models.BooleanField(default=True)
      date_posted = models.DateTimeField(default=datetime.now)

      def __str__(self):
          return self.property_title

views.py

def seller(request):

    if request.method == 'POST':
        form = SellerForm(request.POST, request.FILES)
        if form.is_valid():
            form = form.save(commit=False)
            form.user = request.user
            form.save()
            return redirect('homes')
    else:
        form = SellerForm()

    return render(request, 'sellers/seller_form.html', {
        'form': form
    })

tests.py

class TestModelHome(TestCase):

    def setUp(self):
        Home.objects.create(
            property_title='The Cliff',
            address='455 Paper Mill Road',
            city='Atlanta',
            state='GA',
            zipcode='30046',
            information='This is just a test, no worry, be happy',
            price=125000,
            bedrooms=2,
            bathrooms=1.5,
            garage=1,
            homesize=1111,
            landsize=2.1,
            is_posted=True
        )

    def test_check_items(self):
        home = Home()
        home.property_title = 'The Cliff'
        home.city = 'Atlanta'
        home.state = 'GA'
        home.zipcode = '30046'
        home.save()

        record = Home.get(pk=1)
        self.assertEqual(record, home)

0 个答案:

没有答案