postgresql - Django foreign keys : settings.AUTH_USER_MODEL keeps giving null for form.save -


i have read articles , tried implement in models.

user = models.foreignkey(settings.auth_user_model)  

but whenever try save form user states value cant null. assume because not passing/picking auth_user

for example:

class degree(models.model):      user = models.foreignkey(settings.auth_user_model)      degreelevel = models.charfield(max_length = 50)       areaofstudy = models.charfield(max_length = 50) 

i have feeling on user creation need create signal models use user = models.foreignkey(settings.auth_user_model) struggling find resource deals issue start finish, not sure going wrong.

any great...

this question relates profiles_details.id not exist django

thank you.

i have tried :

user = models.foreignkey(settings.auth_user_model, null=false, on_delete=models.cascade, verbose_name=_(u"user")) got error message when running migrations of :

 nameerror: name '_' not defined 

if take verbose nameout , try migrate get: error:

you trying add non-nullable field 'user' details without default; can't (the database needs populate existing rows). please select fix: 1) provide one-off default (will set on existing rows) 2) quit, , let me add default in models.py

also looked @ https://docs.djangoproject.com/en/dev/topics/auth/customizing/#referencing-the-user-model

but didnt understand with:

 django.contrib.auth.get_user_model(). 

i see several details target in question:

  1. if want user field not mandatory, should change to:

     user = models.foreignkey (..., null=true,...) 
  2. if want solve problem _ not being recognised, should add following import on top of file:

      django.utils.translation import ugettext _ 
  3. if want populate user field in view, follows:

     django.views.generic import view   class myview(view):      ...       def get(request, *args, **kwargs):           myobject = degree.objects.get(...) # query           myobject.user = request.user          myobject.save()           return ... 

    assuming view handles request , wants assign current user object user.

  4. about how use: django.contrib.auth.get_user_model here example

    from django.contrib.auth import get_user_model  ...   def create_user(username, first_name, last_name, *args, **kwargs):      user_class = get_user_model()     new_user = user_class.objects.create(username=username,         first_name=first_name, last_name=last_name) 

    basically, get_user_model function returns class configured in settings.auth_user_model. can access, example, managers (e.g. objects) , query or create new instances.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -