0

I'm working on a project using Python(3.7) and Django(2.2) in which I have implemented my models for multiple user types with custom user model as the base model. Everything working fine except the admin side, I have register these modles to admin but when I try to add an object from admin interface it's giving an error.

Here's what I have tried so far:

From models.py:

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=254, unique=True)
    title = models.CharField(max_length=255, blank=False)
    user_type = models.CharField(max_length=255, choices=USER_TYPE, blank=False)
    gender = models.CharField(max_length=255, choices=CHOICES, blank=False)
    contenst = models.CharField(max_length=255, blank=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    last_login = models.DateTimeField(null=True, blank=True)
    date_joined = models.DateTimeField(auto_now_add=True)

    USERNAME_FIELD = 'email'
    EMAIL_FIELD = 'email'
    REQUIRED_FIELDS = ['password']

    objects = UserManager()

    def get_absolute_url(self):
        return "/users/%i/" % (self.pk)


class PersonalBelow18(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
    dob = models.DateField(blank=False)
    customer_id = models.BigIntegerField(blank=False)
    collection_use_personal_data = models.BooleanField(blank=False)
    reference_identities = models.ForeignKey(Identities, blank=False, on_delete=models.CASCADE, related_name='refs')

    def __str__(self):
        return self.user.email+'\'s account with ' + str(self.customer_id)


class PersonalAbove18(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    dob = models.DateField(blank=False)
    customer_id = models.BigIntegerField(blank=False)
    contact_email = models.EmailField(blank=False)
    reference_identities = models.ForeignKey(Identities, blank=False, on_delete=models.CASCADE)
    contact_no = PhoneNumberField(blank=True, help_text='Phone number must be entered in the'
                                                        'format: \'+999999999\'. Up to 15 digits allowed.')

    collection_use_personal_data = models.BooleanField(blank=False)

    def __str__(self):
        return self.user.email+'\'s account with ' + str(self.customer_id)


class Parent(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    contact_email = models.EmailField(blank=False)
    customer_id = models.BigIntegerField(blank=True)
    contact_no = PhoneNumberField(blank=True, help_text='Phone number must be entered in the'
                                                        'format: \'+999999999\'. Up to 15 digits allowed.')
    collection_use_personal_data = models.BooleanField(blank=False)


class GroupContactPerson(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    contact_email = models.EmailField(blank=False)
    customer_id = models.BigIntegerField(blank=False)
    contact_no = PhoneNumberField(blank=True, help_text='Phone number must be entered in the'
                                                        'format: \'+999999999\'. Up to 15 digits allowed.')
    department = models.CharField(max_length=255, blank=False)
    address = models.TextField(max_length=255, blank=False)

and here's how I register these models to admin:

From admin.py:

class UserAdmin(BaseUserAdmin):
    fieldsets = (
        (None, {'fields': ('email', 'password', 'title', 'user_type',
                           'gender', 'contenst', 'last_login')}),
        ('Permissions', {'fields': (
            'is_active', 
            'is_staff', 
            'is_superuser',
            'groups', 
            'user_permissions',
        )}),
    )
    add_fieldsets = (
        (
            None,
            {
                'classes': ('wide',),
                'fields': ('email', 'password1', 'password2')
            }
        ),
    )

    list_display = ('email', 'title', 'is_staff', 'last_login')
    list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')
    search_fields = ('email',)
    ordering = ('email',)
    filter_horizontal = ('groups', 'user_permissions',)

admin.site.register(User, UserAdmin)
admin.site.register(PersonalBelow18)
admin.site.register(PersonalAbove18)
admin.site.register(Parent)
admin.site.register(GroupContactPerson)

The Parent and GroupContactPerson models are working well on admin side but the when I try to add an object for PersonalBelow18 & PersonalAbove18 models, it gives the following error as:

TypeError: str returned non-string (type int)

4
  • Please post the full stack trace for the error. Commented Dec 14, 2019 at 5:57
  • Also, is the Identity model str method declared? If there any mistake in it, it can be the reason of the error due to the FK displayed in PersonalBelow18 admin interface. Commented Dec 14, 2019 at 10:13
  • @IgorBelkov I have added the __str__ method to all models above. Commented Dec 14, 2019 at 15:57
  • @Abdul Rehman as I can see in the answer the problem was in Identity model, as I mentioned ^_^ It's typical for Django in case of this error, so you always need to check not only the models in the app, but also all the connections. Glad you solved it. Commented Dec 16, 2019 at 11:16

1 Answer 1

1

Here's how I debug this problem in these models:

  1. I start removing all fields one-by-one
  2. Remove a field from model & form and perform migrations
  3. Then test the admin

Then I found that when I removed the reference_identities field it start working, so I get that this model was returning an integer, so I fixed that model and it fix the issue.

In short, it's a good approach to find a path to the actual place of problem by removing fields one-by-one and test the admin.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.