0

I see that similar to this has been asked before but I would like to know if there was a simpler way to achieve this.

Also followed this blog post.

A sample Model is given below.

class Post (models.Model):
    name = models.CharField(max_length=1000, help_text="required, name of the post")
    description = models.TextField(blank=True)
    created_datetime = models.DateTimeField(auto_now_add=True, editable=False)
    modified_datetime = models.DateTimeField(auto_now=True, editable=False)
    custom_hashed_url = models.CharField(unique=True, max_length=1000, editable=False)

def save(self, *args, **kwargs):
        #How to save User here?
        super(Model, self).save()

Isn't it possible to send current logged in user to the Model before calling save()?

In the view:

if request.method == 'POST':

if not errors:

    f = PostForm(request.POST)

    f.save()
Community
  • 1
  • 1
  • 1
    I have been looking for a solution to this problem. My user is logged in, I have the foreignkey setup, but it gives me an error `Cannot assign None: "MyModel.user" does not allow null values.` –  Mar 17 '11 at 00:23

1 Answers1

1

Given that you've tagged this with django-admin, I'll assume you're wishing to save the User who is modifying the object via the admin interface. (You can't really do it in your model's save method, because it doesn't necessarily have access to a User object -- e.g. what if you're saving an object from a shell interface?)

To do this within Django's admin, simply override the save_model method of your ModelAdmin:

class PostAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.user = request.user
        obj.save()

admin.site.register(Post, PostAdmin)

Of course, you would need to actually add a ForeignKey named user to your model for that to work...

DrMeers
  • 4,117
  • 2
  • 36
  • 38
  • I see. So then, how to do people save User in their models if there is a User Foreignkey ? –  Mar 15 '11 at 01:54
  • You just need to add `user = models.ForeignKey(User)` to your model. Then the above code should work fine. – DrMeers Mar 15 '11 at 02:02
  • I agree but this is from the admin interface. Is there any easy way to do that from my app, using django's auth. –  Mar 15 '11 at 13:55
  • Only if you have access to the request object, in which case solutions don't come much easier than the original link you posted -- a model form which saves a reference to the request and extracts the user from that upon save. – DrMeers Mar 15 '11 at 20:25
  • I have been looking for a solution to this problem. My user is logged in, I have the foreignkey setup, but it gives me an error `Cannot assign None: "MyModel.user" does not allow null values.` –  Mar 17 '11 at 00:23
  • Where/how are you saving your object? In admin, using a `ModelAdmin` with `save_model` as described above? If elsewhere, please post your code. – DrMeers Mar 17 '11 at 00:56
  • I cannot post code properly on this so here is the Google Doc... http://goo.gl/CdLsZ . ... I am not using it in the Admin section or ModelAdmin. I want my website to save users who posted stuff if they are logged in. –  Mar 17 '11 at 01:15
  • Isn't it possible to send current logged in user to the Model before calling save()? –  Mar 17 '11 at 01:24
  • @User007: there is no such thing as "the current logged in user" -- there could be hundreds of users currently logged in, it depends which one made the request, which is why you need the request object. – DrMeers Mar 17 '11 at 03:13