Why Django stops execution if reverse relation does not exist?

Andrius Gečys

I have a common situation: a User model and an Employee model which is related to User by one-to-one relation on user_id. My user model is customized and one of customizations is the method "is_employee":

class User(AbstractUser):
    def is_employee(self):
        print(self.id)
        print(self.employee is not None)
        return self.employee is not None
    is_employee.boolean = True
    is_employee.short_description = _('Employee status')


class Employee(AddressMixin, models.Model):
    user        = OneToOneField(User, 
                                primary_key=True, 
                                related_name='employee',
                                on_delete=CASCADE)
    phone_no    = models.CharField(max_length=20, 
                                   blank=True, 
                                   default='')
    mobile_no   = models.CharField(max_length=20, 
                                   blank=True, 
                                   default='')

I use User.is_employee in the admin site on list_display. The problem is that the function User.is_employee only returns a result if there is an Employee for that user. If there is not then it stops executing where it first meets the call self.employee ant thereby returns None. And here is what I see on the admin: Employee status for the last user last user is not False but None

Sorry if the question was messy. I am quite new to Django and this is my first post on this site as well. Thank you in advance.

Alasdair

Doing self.employee raises an ObjectDoesNotExist exception when there is not a related employee. In your case, the exception is being caught and - is being displayed in the table.

You can use hasattr to check whether the user has a related employee without raising an exception.

def is_employee(self):
    return hasattr(self, 'employee')

See the docs on one-to-one relationships for more info.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django: relation does not exist

Django migrations: relation does not exist

Django: Relation does not exist in Postgresql

Relation does not exist - Django & Postgres

Why does withCallingHandlers still stops execution?

Django + posgtres relation does not exist error

ProgrammingError: relation "django_session" does not exist

ProgrammingError: relation "django_site" does not exist

Relation does not Exist/ Programming Error in django

Django issue: relation "django_site" does not exist

Django column "name" of relation "django_content_type" does not exist

Why am i getting "relation does not exist" when it does?

Why does a breakpoint in CompletableFuture stops execution in Main thread as well?

Why does a breakpoint in CompletableFuture stops execution in Main thread as well?

How to access the a reverse relation of a reverse relation django

Relation *tablename* does not exist

relation "old" does not exist

Relation does not exist PLPGSQL

Relation does not exist in django admin site after migrations

Relation does not exist error in Django after postgresql integration

Python Django - Internal Error ProgrammingError relation does not exist

Django error: relation "users_user" does not exist

relation "blog_blog" does not exist - error in Django app

relation "account_emailaddress" does not exist - django error

executing raw sql in django error: relation does not exist

django.db.utils.ProgrammingError: relation "..." does not exist

django.db.utils.ProgrammingError: relation does not exist with recursive model

Django - Relation "relation" does not exist. Cannot run python manage.py migrate?

Django test fails with 'django.db.utils.ProgrammingError: relation "django_content_type" does not exist'