python - Django one-to-one reverse relationship DoesNotExist -
i've got odd problem here, one-to-one relationship doesn't seem working in reverse. it's easiest explain code.
i have extended default django user add timezone, so:
#this model holds custom user fields class taskuser(models.model): user = models.onetoonefield(user, related_name="task_user") timezone = models.charfield(max_length=50, default="utc")
i migrated using south, no problems far. it's shown inline in admin, again no problems. used syncdb since haven't used south user before, synced else no problems.
so, according documentation should have field task_user on user objects, references taskuser object.
if go django shell, not case. (see in[6])
in [1]: django.contrib.auth.models import user in [2]: current_user = user.objects.get(pk=1) in [3]: current_user? type: user string form:callum file: /usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py docstring: users within django authentication system represented model. username, password , email required. other fields optional. in [4]: field in current_user._meta.fields: print field.name ...: id password last_login is_superuser username first_name last_name email is_staff is_active date_joined in [5]: dir(current_user) out[5]: ['doesnotexist', 'meta', 'multipleobjectsreturned', 'required_fields', 'username_field', #i have removed many more field here 'task_user', #and few here 'validate_unique'] in [6]: current_user.task_user --------------------------------------------------------------------------- doesnotexist traceback (most recent call last) /usr/local/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in <module>() ----> 1 current_user.task_user /usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.pyc in __get__(self, instance, instance_type) 277 setattr(instance, self.cache_name, rel_obj) 278 if rel_obj none: --> 279 raise self.related.model.doesnotexist 280 else: 281 return rel_obj doesnotexist:
i'm bit confused result - seems object has task_data field somewhere, not relationship? i'm not sure how can access , avoid error.
thanks in advance can offer.
when specify related_name, you’re not adding field user
model. instead, kind of descriptor created won’t visible in user
’s fields
property. having field defined in taskuser
model in such way means not possible have taskuser
instance without associated user, possible have user
instance without associated taksuser
instance (the same stays true foreignkey
relations), need ensure taskuser
instance created; won’t done automatically.
Comments
Post a Comment