Ошибка ограничения NOT NULL: profiles_tweet.author_id
Я хочу сохранить твит с ожидающим состоянием, но это дает ошибку целостности.
И еще я хочу просмотреть все твиты автора, но я не могу их отфильтровать.
Я хочу, чтобы если какой-либо твит автора что-то хранился как ожидающий в базе данных. и я хочу его просмотреть в своем профиле.
Из models.py
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
USER_TYPE_CHOICES = (
('AUTHOR', 'author'),
('EDITOR', 'editor'),
('PUBLISHER', 'publisher'),
('SUPERVISOR', 'supervisor'),
('ADMIN', 'admin'),
)
user_type = models.CharField(choices=USER_TYPE_CHOICES,max_length=64, default='student')
favorite_color = models.CharField(max_length=50)
bio = models.TextField(null=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['favorite_color']
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
class Tweet(models.Model):
author = models.ForeignKey(MyUser,on_delete=models.CASCADE)
text = models.CharField(max_length=140)
# author_email = models.CharField(max_length=200)
created_at = models.DateTimeField(auto_now_add=True)
published_at = models.DateTimeField(null=True)
STATE_CHOICES = (
('pending','pending'),
('published', 'published'),
('rejected','rejected'),
)
state = models.CharField(max_length=15, choices=STATE_CHOICES)
class Meta:
permissions = (
("can_approve_or_reject_tweet","Can approve or reject tweets"),
)
# def __str__(self):
# return self.name
def get_absolute_url(self):
return reverse("Tweet_detail", kwargs={"pk": self.pk})
class Comment(models.Model):
tweet = models.ForeignKey(Tweet,on_delete=models.CASCADE)
text = models.CharField(max_length=300)
created_at = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.text
Из forms.py
class TweetForm(forms.ModelForm):
class Meta:
model = Tweet
fields = ["text"]
widgets = {
'text': forms.Textarea(attrs={'cols': 50, 'rows': 3}),
}
Из views.py
@user_passes_test(is_author)
def post_tweet(request, tweet_id=None):
tweet = None
if tweet_id:
tweet = get_object_or_404(Tweet, id=tweet_id)
if request.method == 'POST':
form = TweetForm(request.POST, instance=tweet)
if form.is_valid():
print("VALID")
new_tweet = form.save(commit=False)
new_tweet.state = 'pending'
new_tweet.save()
# send_review_email()
return HttpResponseRedirect(reverse('thank-you'))
else:
form = TweetForm(instance=tweet)
return render(request, 'author/post_tweet.html',{'form': TweetForm(instance=tweet)})
@user_passes_test(is_author)
def thank_you(request):
tweets_in_queue = Tweet.objects.filter(state='pending')
return render(request, 'author/thank_you.html',{'tweets_in_queue': tweets_in_queue})