Friday, March 1, 2013

Re: Problem UserAdmin with GenericStackedInline in Django 1.5c2

Hm.. I am not extending User Model.

I lost one class, this is real example:

Create APPs: core

SETTINGS:

#Django 1.5

AUTH_USER_MODEL = 'core.Partners'



MODELS:

# -*- coding: utf-8 -*-

from django.db import models

from django.contrib.contenttypes.models import ContentType

from django.contrib.contenttypes import generic

from django.utils.translation import ugettext_lazy as _

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

from django.conf import settings


class CreateModified(models.Model):

    date_create = models.DateTimeField(auto_now_add=True)

    date_modified = models.DateTimeField(auto_now=True)

    user_create_fk = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='%(app_label)s_%(class)s_usercreate', blank=True,null=True)

    user_modified_fk = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='%(app_label)s_%(class)s_userchange', blank=True,null=True)

    class Meta:

        abstract = True


class Addresses(CreateModified):

    """

    Fields extends from CreateModified

    date_create = models.DateTimeField(auto_now_add=True)

    date_modified = models.DateTimeField(auto_now=True)

    THIS IS PROBLEM:

    user_create_fk = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='%(app_label)s_%(class)s_usercreate', blank=True,null=True)

    user_modified_fk = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='%(app_label)s_%(class)s_userchange', blank=True,null=True)

    """

    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(CreateModified, AbstractBaseUser, PermissionsMixin):

    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"



FORMS:

# -*- coding: utf-8 -*-

from django import forms

from django.contrib.auth.forms import ReadOnlyPasswordHashField

from core.models import Partners, Addresses

from django.forms import widgets

from django.utils.translation import ugettext_lazy as _


class PartnersCreationForm(forms.ModelForm):

    """A form for creating new users. Includes all the required fields, plus a repeated password."""

    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)

    password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)


    class Meta:

        model = Partners

        fields = ('login',)


    def clean_password2(self):

        # Check that the two password entries match

        password1 = self.cleaned_data.get("password1")

        password2 = self.cleaned_data.get("password2")

        if password1 and password2 and password1 != password2:

            raise forms.ValidationError("Passwords don't match")

        return password2


    def save(self, commit=True):

        # Save the provided password in hashed format

        user = super(PartnersCreationForm, self).save(commit=False)

        user.set_password(self.cleaned_data["password1"])

        if commit:

            user.save()

        return user


class PartnersChangeForm(forms.ModelForm):

    """A form for updating users. Includes all the fields on

    the user, but replaces the password field with admin's

    password hash display field.

    """

    password = ReadOnlyPasswordHashField()


    class Meta:

        model = Partners


    def clean_password(self):

        # Regardless of what the user provides, return the initial value.

        # This is done here, rather than on the field, because the

        # field does not have access to the initial value

        return self.initial["password"]



ADMIN:

# -*- coding: utf-8 -*-

from django.contrib import admin

from core.models import Partners, Addresses

from django.contrib.auth.admin import UserAdmin

from core.forms import PartnersChangeForm, PartnersCreationForm

from django.contrib.contenttypes import generic



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)



if the class Addresses not extends class CreateModified the example works correctly
The problem is the existing 2 ForeignKey field in CreateModified to Partner:

user_create_fk

user_modified_fk


and this field is extend in Addresses.

How to resolve this problem? Each class must contain a field of CreateModified Class?
Only Partners not contains field of CreateModified.











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.
 
 

No comments:

Post a Comment