Home > database >  Django Crispy Form filter by is_staff
Django Crispy Form filter by is_staff

Time:10-13

I have a form to upload article posts but how would I filter the author so it just shows users who have the is_staff role, I also need it to show authors as usernames rather than email adresses

Here is my forms.py

class ArticleForm(forms.ModelForm):
    tags = TagField(required=False, widget=LabelWidget)
    class Meta:
        model = Article
        fields = ('article_name', 'author', 'content', 'tags', 'status')

Here is my views.py

class ArticleCreateView(LoginRequiredMixin, CreateView):
    model = Article
    form_class = ArticleForm
    template_name = 'main_website/new_article.html'  # <app>/<model>_<viewtype>.html   

    def form_valid(self, form):
        form.instance.author = self.request.user
        form.save()
        return super().form_valid(form)
    
    def get_context_data(self, **kwargs):
        articles = Article.objects.filter(status=1).order_by('-date_posted')[:2]
        context = super().get_context_data(**kwargs)
        context['articles'] = articles
        context['title'] = 'New Article'
        return context

Here is my models.py

class Article(models.Model):

    class Status(models.IntegerChoices):
        Draft = 0
        Published = 1

    article_name = 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='news_updates')
    updated_on = models.DateTimeField(auto_now=True)
    content = models.TextField()
    tags = TaggableManager()
    date_posted = models.DateTimeField(default=timezone.now)
    status = models.IntegerField(choices=Status.choices, default=Status.Draft, help_text=_('Decide whether you want to Publish the news article or save it as a Draft'))

    class Meta:
        ordering = ['-date_posted']

    def __str__(self):
        return self.article_name
    
    def get_absolute_url(self):
        return reverse("main_website_article_detail", args=[str(self.slug)])

    def save(self, *args, **kwargs):
        self.slug = slugify(self.article_name)
        super(Article, self).save(*args, **kwargs)

CodePudding user response:

class ArticleForm(forms.ModelForm):
    tags = TagField(required=False, widget=LabelWidget)
    author = forms.ModelChoiceField(queryset=User.objects.filter(is_staff=True))

    class Meta:
        model = Article
        fields = ('article_name', 'author', 'content', 'tags', 'status')

And for your second question (taken from django docs):

The str() method of the model will be called to generate string representations of the objects for use in the field’s choices. To provide customized representations, subclass ModelChoiceField and override label_from_instance. This method will receive a model object and should return a string suitable for representing it. For example:

from django.forms import ModelChoiceField

class MyModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return "My Object #%i" % obj.id
  • Related