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:
if want
user
field not mandatory, should change to:user = models.foreignkey (..., null=true,...)
if want solve problem
_
not being recognised, should add following import on top of file:django.utils.translation import ugettext _
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.
about how use:
django.contrib.auth.get_user_model
here examplefrom 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 insettings.auth_user_model
. can access, example, managers (e.g.objects
) , query or create new instances.
Comments
Post a Comment