0

So, I have an idea to build a web app. I am new to this business but am a programmer. I liked Python and thought let me start with Django. I ran into a problem with the built-in Django User Auth system as pointed by this question of mine on SO.

In short, I am using Django's built-in User auth and thought that it will easily fill my user field (the foreignkey) in my model but it does not. I have Googled and asked questions but only got a very convoluted answer or that I have to use the Admin section if I want anything like that.

My simple need is that whenever user saves anything in their profile, the user field should get populated so that I can reference it. To me, it sounds like the most basic need for any web app.

Am I wrong. I need advice. If Django is not good for this then I am ready to learn any other good framework or platform if need be.

Community
  • 1
  • 1

1 Answers1

2

In short, I am using Django's built-in User auth and thought that it will easily fill my user field (the foreignkey) in my model but it does not.

This won't just magically happen. You will have make it happen. Models are separated from the request so this would happen in your view, or a model method (perhaps save) that is passed the active user.

MVC / MTV design separates the database from the view / control logic. I don't see what framework has to do with this: unless you write the functionality yourself, the database doesn't know what to do with some User table and the currently logged in user (also separated from the data). Building in this functionality would inconvenience a lot of people as well...

In general, the python/django philosophy is: Explicit is better than implicit.

Now the solutions:

If you wanted this behavior in the view, for any form, you could potentially write:

instance = MyForm.save(commit=False) # commit=False prevents DB save.
instance.user = request.user
instance.save()

You could overwrite the save method on the model that accepts an optional user argument as well.

def save(self, *args, **kwargs):
    user = kwargs.pop(user, None)
    self.user = user
    super(MyModel, self).save(*args, **kwargs)

 mymodel = MyModel()
 mymodel.save(user=request.user)
Community
  • 1
  • 1
Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
  • When I say `mymodel = MyModel()` it says MyModel is not defined. –  Mar 19 '11 at 17:44
  • Where should I put the last two lines of code? `mymodel = MyModel() mymodel.save(user=request.user)` –  Mar 20 '11 at 02:49
  • Do I need to import something in my views.py? –  Mar 20 '11 at 03:00
  • This is pseudo code. I can't read into your mind and know what you call your models (unfortunately). Save your modelform with commit=False, or override your Model save method (as shown above) – Yuji 'Tomita' Tomita Mar 20 '11 at 04:10
  • Sorry about that. I think the problem is that my `if form.is_valid():` check is failing. the control never goes inside that `if` statement. Weird. –  Mar 20 '11 at 18:15
  • Then my guess is you need to set up your modelform to remove `User` as a field. Set the modelform class Meta exclude attribute to remove your user, since you will be supplying the user yourself later. Without knowing the exact `form.errors` that's about as much i can do! – Yuji 'Tomita' Tomita Mar 20 '11 at 19:31