2 years ago

#60830

test-img

RayyanS

Django objects.all() not updating without server restart

django objects.all() only seems to be updating after server restart. For example, I would make a category called 'Education' and it shows up on the admin site, but when I'm using in the form, it will not show up unless I restart the live server.

forms.py:

def tuplelistmaker(list):
    output = []
    for x in list:
        newtuple = (x.pk, x)
        output.append(newtuple)
    print(output)
    return output

class PostForm(forms.Form):
    title = forms.CharField(max_length=100, label='Post Title')
    short_description = forms.CharField(widget=forms.Textarea(attrs={"rows":3, "cols":100}))
    content = forms.CharField(widget=CKEditorWidget())
    status = forms.NullBooleanField(label='Ready to Publish?')
    image = forms.ImageField(label='Select a cover image:')
    captioned_image = forms.ImageField(label='Select a captionable image:')
    caption = forms.CharField(max_length=200)
    category = forms.ChoiceField(choices = tuplelistmaker(list(PostCategory.objects.all())))
    embed = forms.CharField(widget=forms.Textarea(attrs={"rows":3, "cols":100}))
    tags = forms.CharField(widget=forms.Textarea(attrs = {"rows":1, "cols":150}))

models.py:

class PostCategory(models.Model):
    category = models.CharField(max_length=100)
    categorySlug = models.SlugField(max_length=100)

    def __str__(self):
        return self.category

class Post(models.Model):
    title = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(max_length=200, unique=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name= 'blog_posts')
    short_description = models.TextField()
    updated_on = models.DateTimeField(auto_now=True)
    content = RichTextField()
    created_on = models.DateTimeField(auto_now=True)
    status = models.IntegerField(choices=Status, default=0)
    cover_image = models.ImageField(upload_to = 'coverimages', null =True, blank = True)
    captioned_image = models.ImageField(upload_to = 'captionedimages', null=True, blank = True)
    caption = models.CharField(max_length=300)
    featured = models.IntegerField(choices=Featured, default=1)
    category = models.ForeignKey(PostCategory, on_delete=models.CASCADE, null=True, blank=True)
    embedded_code = models.TextField(blank=True, null=True, default='null')
    tags = models.TextField(blank=True, null=True)

    class Meta:
        ordering = ['-created_on']
    
    def __str__(self):
        return self.title

python

django

blogs

0 Answers

Your Answer

Accepted video resources