33👍
✅
http://code.djangoproject.com/wiki/CookBookNewformsAdminAndUser
Involves implementing save methods on your ModelAdmin objects.
4👍
You need to specify a default for the field, in this case a method call that gets the current user (see the auth documentation to get the current user).
- [Django]-Determine variable type within django template
- [Django]-Django import error – No module named core.management
- [Django]-ManyRelatedManager object is not iterable
2👍
I did it by overriding the save_model method in django admin. Since you mentioned that you wanted to use the Django admin interface. So, I think my solution will be the most appropriate one. The solution goes like this:
- First register the Contact model in the admin by excluding the ‘created_by’ field
@admin.register(Contact) class Notice(admin.ModelAdmin): list_display = ('name', 'created_by') exclude = ('created_by', )
- Then override the save_model method in the same class Notice
def save_model(self, request, obj, form, change,): obj.created_by = request.user super().save_model(request, obj, form, change)
- [Django]-Django: Overriding AND extending an app template
- [Django]-Naming convention for Django URL, templates, models and views
- [Django]-Django 1.5 index page
1👍
I really wanted a better solution than the ones I found elsewhere, so I wrote some middleware – implementation on the Populate user Id question. My solution requires no changes to any form or view.
- [Django]-Dynamically limiting queryset of related field
- [Django]-What is related_name used for?
- [Django]-Django – How to prepopulate admin form fields
Source:stackexchange.com