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:
```
```
Preferably I would like the field to just be hidden when editing or creating a new `BlogPost`.
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`.
No any search results
You already invited:
2 Answers
ahmad
Upvotes from:
Mr David
Upvotes from: