I'd like to do something like this:
class Task(models.Model): ... created_by = models.ForeignKey(User, **default=[LoggedInUser]** blank=True, null=True, related_name='created_by')
Is this possible? I couldn't find what's the proper way to get the logged in user, apart from doing request.user, in a view, which doesn't seem to work here.
PS_ I realise I could initialize the Model data by other means, but I think this is the cleanest way.
If you want to achieve this within the admin interface, you can use the save_model method. See below an example:
class List(models.Model): title = models.CharField(max_length=64) author = models.ForeignKey(User) class ListAdmin(admin.ModelAdmin): fields = ('title',) def save_model(self, request, obj, form, change): obj.author = request.user obj.save()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With