Wednesday, February 27, 2013

Re: Problem UserAdmin with GenericStackedInline in Django 1.5c2

Hi maybe you forget this 

    # Re-register UserAdmin  admin.site.unregister(User)  admin.site.register(User, UserAdmin)
https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#extending-the-existing-user-model


On Wed, Feb 27, 2013 at 2:35 AM, Witold Greń <witold.gren@gmail.com> wrote:
Hi, I have a problem UserAdmin with GenericStackedInline in Django 1.5c2.

#model.py

from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, PermissionsMixin

from django.conf import settings

#etc...


class Addresses(models.Model):

    """

    Dane adresowe.

    """

    name = models.CharField(max_length=200, verbose_name='Nazwa')

    street = models.CharField(max_length=200, verbose_name='Ulica')

    no_house = models.IntegerField(verbose_name='Numer budynku')

    no_local = models.IntegerField(verbose_name='Numer lokalu', blank=True, null=True)

    postal_code = models.CharField(max_length=200, verbose_name='Kod pocztowy')

    city = models.CharField(max_length=200, verbose_name='Miasto')

    region = models.CharField(max_length=200, verbose_name='Województwo')

    content_type = models.ForeignKey(ContentType,verbose_name='Rodzaj modelu', related_name="content_type_%(class)s",)

    object_id = models.PositiveIntegerField(verbose_name='Rodzaj obiekt', db_index=True)

    content_object = generic.GenericForeignKey('content_type', 'object_id')



class Partners(AbstractBaseUser, PermissionsMixin):

    """

    Użytkownik

    """

    login = models.CharField(max_length=200, verbose_name='Login', help_text=_('Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters'), unique=True, db_index=True)

    company = models.BooleanField(verbose_name='Firma/Prywatny')

    company_name = models.CharField(max_length=200, verbose_name='Nazwa firmy', blank=True, null=True)

    www = models.URLField(verbose_name='Strona internetowa', blank=True, null=True)

    name = models.CharField(max_length=200, verbose_name='Imię', blank=True, null=True)

    surname = models.CharField(max_length=200, verbose_name='Nazwisko', blank=True, null=True)

    phone = models.CharField(max_length=200, verbose_name='Telefon', blank=True, null=True)

    email = models.EmailField(verbose_name='Email', blank=True, null=True)

    regulations = models.BooleanField(verbose_name='Regulamin', default=True)

    date_of_birth = models.DateField(verbose_name='Data urodzenia', blank=True, null=True)

    is_staff = models.BooleanField(_('staff status'), default=True, help_text=_('Designates whether the user can log into this admin site.'))

    is_active = models.BooleanField(_('active'), default=True, help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))

    date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)

    

    objects = PartnersManager()


    USERNAME_FIELD = 'login'

    REQUIRED_FIELDS = []


    def get_full_name(self):

        return '%s %s' % (self.name, self.surname)


    def get_short_name(self):

        return self.login


    def __unicode__(self):

        return self.login


    class Meta:

        verbose_name = "Użytkownik"

        verbose_name_plural = "Użytkownicy"

        ordering = ['id']

        get_latest_by = "id"



#admim.py:

from django.contrib.auth.admin import UserAdmin

from core.forms import PartnersChangeForm, PartnersCreationForm

#etc...


class AddressAdminInline(generic.GenericStackedInline):

    model = Addresses

    ct_field = "content_type"

    ct_fk_field = "object_id"

    extra = 1

    max_num = 1



class PartnersAdmin(UserAdmin):

    form = PartnersChangeForm

    add_form = PartnersCreationForm

    list_display = ('login', 'name', 'surname', 'is_superuser')

    list_filter = ('is_superuser',)

    save_on_top = True

    inlines = [

        AddressAdminInline

    ]

    fieldsets = (

        (None, {

            'fields': ('login', 'password')

        }),

        ('Personal info', {

            'fields': ('email', 'www', 'date_of_birth')

        }),

        ('Personal info 2', {

            'fields': ('company','company_name', 'name', 'surname', 'phone', 'regulations' )

        }),

        ('Permissions', {

            'fields': ('is_superuser', 'is_staff', 'is_active', 'groups', 'user_permissions')

        }),

        ('Important dates', {

            'fields': ('last_login',)

        }),

    )

    add_fieldsets = (

        (None, {

            'classes': ('wide',),

            'fields': ('login', 'password1', 'password2')}

        ),

    )

    search_fields = ('login',)

    ordering = ('login',)

    filter_horizontal = ('groups', 'user_permissions')


admin.site.register(Partners, PartnersAdmin)

admin.site.register(Addresses)


when run server used"./manage.py runserver" and go to User in DjangoAdminPanel i show:

Exception at /admin/core/partners/1/

    <class 'core.models.Addresses'> has more than 1 ForeignKey to <class 'core.models.Partners'>
Request Method:GET
Request URL:http://localhost:8000/admin/core/partners/1/
Django Version:1.5c2
Exception Type: Exception
Exception Value:
    <class 'core.models.Addresses'> has more than 1 ForeignKey to <class 'core.models.Partners'>
Exception Location:/Library/Python/2.7/site-packages/django/forms/models.py in _get_foreign_key, line 825
Python Executable:/usr/bin/python
Python Version: 2.7.2

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  92.                     response = middleware_method(request)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/Users/user/projects/project-name/project-name/urls.py" in <module>
  6. admin.autodiscover()
File "/Library/Python/2.7/site-packages/django/contrib/admin/__init__.py" in autodiscover
  29.             import_module('%s.admin' % app)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/Users/user/projects/project-name/core/admin.py" in <module>
  125. admin.site.register(Partners, PartnersAdmin)
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in register
  98.                 validate(admin_class, model)
File "/Library/Python/2.7/site-packages/django/contrib/admin/validation.py" in validate
  189.             validate_inline(inline, cls, model)
File "/Library/Python/2.7/site-packages/django/contrib/admin/validation.py" in validate_inline
  200.     fk = _get_foreign_key(parent_model, cls.model, fk_name=cls.fk_name, can_fail=True)
File "/Library/Python/2.7/site-packages/django/forms/models.py" in _get_foreign_key
  825.             raise Exception("%s has more than 1 ForeignKey to %s" % (model, parent_model))

Exception Type: Exception at /admin/core/partners/1/
Exception Value: <class 'core.models.Addresses'> has more than 1 ForeignKey to <class 'core.models.Partners'

Where is the problem? Please, help... ;)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment