Can anyone help me with Django admin forms? I'm trying to automatically set a field when it's saved, but it doesn't seem to work.

Can anyone help me with Django admin forms?
I'm trying to automatically set a field when it's saved, but it doesn't seem to work. 
My understanding was to do the following:
```
# models.py
class BlogPost(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
posted_date = models.DateField(default=timezone.now)
title = models.CharField(max_length=200)
text = models.TextField(max_length=1000)
# in admin.py
class BlogPostAdmin(admin.ModelAdmin):
model = BlogPost
def formfield_for_choice_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'author':
kwargs['queryset'] = User.objects.filter(username=request.user.username)
return super(BlogPostAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

```
Preferably I would like the field to just be hidden when editing or creating a new `BlogPost`.
You already invited:

ahmad

Upvotes from:

@Amos  I don't understand why you don't just set the field in your model's save function and include it in ModelAdmin.exclude. Which field are you trying to set?

Mr David

Upvotes from:

would defining a new `Form` and instantiating it in a `View` (CBV extending `View`) be a good practice? I don't actually want a `FormView`, I am simply making a POST request and data should be validated.

If you wanna answer this question please Login or Register