Data not saved to database
I don't know what's going on here, whenever I try to create a buzz with the status set to Draft
(this is also the default status set on the model) Django returns a 302
response and does not saved it to the database. However, when I change the status to Published
it just saves it normally on the database.
Here's the code to the view
def buzz_create(request):
form = BuzzCreateForm()
if request.method == 'POST':
form = BuzzCreateForm(data=request.POST)
if form.is_valid:
buzz = form.save(commit=False)
buzz.author = request.user
buzz.save()
return redirect(to=reverse('buzz:buzz_list'))
return render(
request=request,
template_name='buzz/create.html',
context={ 'form': form }
)
Here's the code to the model:
class BuzzPublishedManager(models.Manager):
def get_queryset(self):
return (
super().get_queryset().filter(status=Buzz.Status.PUBLISHED)
)
class Buzz(models.Model):
class Status(models.TextChoices):
PUBLISHED = 'PBL', 'Published'
DRAFT = 'DFT', 'Draft'
title = models.CharField(max_length=250)
body = models.TextField()
slug = models.SlugField(max_length=250, unique_for_date='publish')
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
author = models.ForeignKey(
to=settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name='buzzes'
)
status = models.CharField(
max_length=3,
choices=Status,
default=Status.DRAFT
)
published = BuzzPublishedManager()
objects = models.Manager()
class Meta:
verbose_name_plural = 'Buzzes'
ordering = ['-publish']
indexes = [
models.Index(fields=['-publish'])
]
def __str__(self):
return self.title
The objects
in the model was not there before. I tried adding it maybe the data is saved but not queried due to the BuzzPublishedManager
but still no effect.