在Django中扩展管理员用户配置文件

时间:2016-12-12 06:44:25

标签: django oracle django-admin

我想在管理员页面中存储管理员身份验证的数据库用户。为此,我需要修改UserAdmin字段以存储更多列。但我收到以下错误:

/ admin / auth / user / 1 / change /
的DatabaseError ORA-00904:" PROFILE"。" USER_ID":无效标识符

应用名称配置文件,类名称是个人资料。这是代码:

models.py

class profile(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE,null=True,blank=False)
    id = models.FloatField(primary_key=True)
    ip = models.CharField(max_length=20, blank=True, null=True)
    ad = models.CharField(max_length=20, blank=True, null=True)
    soyad = models.CharField(max_length=20, blank=True, null=True)
    username = models.CharField(max_length=200, blank=True, null=True)
    password = models.CharField(max_length=200, blank=True, null=True)
    role = models.FloatField(blank=True, null=True)
    email = models.CharField(max_length=2000, blank=True, null=True)
    parent_username = models.CharField(max_length=2000, blank=True, null=True)
    notif_ask = models.CharField(max_length=3, blank=True, null=True)
    notif_answer = models.CharField(max_length=3, blank=True, null=True)
    department = models.CharField(max_length=100, blank=True, null=True)
    position = models.CharField(max_length=50, blank=True, null=True)
    internal_tel_num = models.CharField(max_length=30, blank=True, null=True)
    mob_tel_num = models.CharField(max_length=50, blank=True, null=True)
    image = models.CharField(max_length=300, blank=True, null=True)
    ata_adi = models.CharField(max_length=20, blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'profile'

    def __str__(self):
        return self.user.username

    def __unicode__(self):
        return self.user.username

    @receiver(post_save, sender=User)
    def create_or_update_user_profile(sender, instance, created, **kwargs):
        if created:
            profile.objects.create(user=instance)
            instance.profile.save()

admin.py

class ProfileInline(admin.StackedInline):
    model = profile
    can_delete = False
    verbose_name_plural = 'profile'
    fk_name = 'user'

class CustomUserAdmin(UserAdmin):
    inlines = (ProfileInline, )

    def get_inline_instances(self, request, obj=None):
        if not obj:
            return list()
        return super(CustomUserAdmin, self).get_inline_instances(request, obj)

admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)

2 个答案:

答案 0 :(得分:0)

models.py

您应该保留 instance.profile.save()if语句在同一行

答案 1 :(得分:0)

ORA-00904:“配置文件”。“ USER_ID”:无效的标识符

该错误来自数据库。 PROFILE是Oracle中的保留字。您不能将其用作表名。