模型未在管理员

时间:2015-11-19 14:56:36

标签: python django django-admin mezzanine

我必须处理与另一个项目重复的项目。第一次使用Python,Django和Mezzanine。

我复制了一个应用程序,因为我需要几乎完全相同的东西,但是使用另一个模板和一些不同的字段。所以我复制了文件夹,几乎将所有需要重命名的事件重命名为CitizenEvents。

我现在搜索我的问题已经有4个小时了,每当有人遇到同样的问题时,通过做我已经做过的事情来解决问题。

仅供参考,我使用的是Python 2.7,Django 1.6,Mezzanine 3(并且无法真正更新)。

所以这就是我现在所拥有的:

/project
----/citizenevents/
    ---- __init__.py
    ---- admin.py
    ---- models.py
    ---- urls.py
    ---- views.py

models.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from mezzanine.galleries.models import Gallery
import datetime
from mezzanine.core.fields import FileField
from mezzanine.core.models import RichText, Displayable


class CitizenEvents(Displayable, RichText):

    CITIZEN_EVENT_CATEGORY_CHOICES = (
        (
            1,'Biliothèque'
        ),(
            2,'Collecte des matières résiduelles'
        ),(
            3,'Échéance - Taxes municipales'
        ),(
            4,'Échéance - Dépôt dossiers Urbanisme'
        ),(
            5,'Échéance - Candidatures comité aide financière'
        ),(
            6,'Séances du conseil municipal'
        ),(
            7,'Ventes de garage'
        )
    )

    s_link = models.URLField(max_length=255, verbose_name='Lien externe', blank=True, null=True)
    s_link_title = models.CharField(max_length=255, verbose_name='Titre du lien', blank=True, null=True)
    s_link_youtube = models.URLField(max_length=255, verbose_name='Lien Youtube', blank=True, null=True)
    b_home = models.BooleanField(verbose_name="Afficher dans le slider de la page d'accueil", blank=True, default=False)
    s_excerpt_home = models.TextField(verbose_name='Resume 150 caracteres', max_length=150, blank=True, null=True)
    eventType = models.IntegerField(verbose_name='Type d\'événement', choices=CITIZEN_EVENT_CATEGORY_CHOICES,default=1)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        from django.core.urlresolvers import reverse
        return reverse('citizenevents:detail', kwargs={'slug':self.slug})

    search_fields = ['title']
    ordering=['-publish_date']

    class Meta:
        verbose_name = 'Événement Ciyoten'
        verbose_name_plural = 'Événements Citoyen'

class CitizenEventDates(models.Model):

    event = models.ForeignKey(CitizenEvents, to_field="id")
    dt_debut = models.DateField(verbose_name='Date de debut', default=None)
    dt_fin = models.DateField(verbose_name='Date de fin', blank=True, null=True)

    ordering = ['dt_debut']

    def __str__(self):
        return self.dt_debut.strftime('%Y-%m-%d')

    class Meta:
        verbose_name = 'Date'
        verbose_name_plural = 'Dates'

    @staticmethod
    def next_event(category=1):
        return CitizenEventDates.objects.all().filter(
            event__eventType = category
        ).filter(
            dt_debut__gte = datetime.date.today()
        ).filter(
            event__status = 2
        ).distinct('event').order_by('event','dt_debut')[:1]

    @staticmethod
    def latest_events_list():
        events = CitizenEventDates.objects.extra(
            select={'class':"(case when dt_debut <= current_date then 'current' else '' end)",'date':"(case when dt_debut < current_date then current_date else dt_debut end)"}
        ).select_related('event').filter(
            event__publish_date__lte = datetime.datetime.now()
        ).filter(
            models.Q(dt_debut__gte = datetime.datetime.today()) | models.Q(models.Q(dt_fin__gte = datetime.datetime.today()) & models.Q(dt_debut__lte = datetime.datetime.today()))
        ).order_by('dt_debut')
        return events

admin.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.contrib import admin
from valdavid.citizenevents.models import CitizenEvents,CitizenEventDates
from mezzanine.core.admin import StackedDynamicInlineAdmin, TabularDynamicInlineAdmin

class CitizenEventDatesInline(TabularDynamicInlineAdmin):
    model = CitizenEventDates
    verbose_name = 'Date'
    verbose_name_plural = 'Dates'

class CitizenEventsAdmin(admin.ModelAdmin):
    EMPTY_DATE_SYMBOL = "-"
    list_display = ('get_first_date','title','eventType','b_home','status')
    #ordering = ('get_first_date','title')
    inlines = (CitizenEventDatesInline,)

    def get_first_date(self, obj):
        if obj.eventdates_set.count() == 0:
            return self.EMPTY_DATE_SYMBOL

        firstDay = obj.eventdates_set.all().order_by('dt_debut')[0]
        return firstDay.dt_debut

    get_first_date.short_description = 'Première occurence'


admin.site.register(CitizenEvents,CitizenEventsAdmin)

在设置中(缩写)

ADMIN_MENU_ORDER = (
    ("Content", ("pages.Page", ("Bibliothèque de médias", "fb_browse"),)),
    ("Actualités", ("news.News", "infotravaux.InfoTravaux", "events.Events", "citizenevents.CitizenEvents", )),
)


INSTALLED_APPS = (
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.redirects",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.sitemaps",
    "django.contrib.staticfiles",
    "mezzanine.boot",
    "mezzanine.conf",
    "mezzanine.core",
    "mezzanine.generic",
    "mezzanine.blog",
    "mezzanine.forms",
    "mezzanine.pages",
    "mezzanine.galleries",
    #"mezzanine.twitter",
    #"mezzanine.accounts",
    #"mezzanine.mobile",
    "widget_tweaks",

    "valdavid.events",
    "valdavid.citizenevents",

    "south",
)

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.contrib.messages.context_processors.messages",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.static",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
    "django.core.context_processors.tz",
    "mezzanine.conf.context_processors.settings",
    "mezzanine.pages.context_processors.page",
)

MIDDLEWARE_CLASSES = (
    "mezzanine.core.middleware.UpdateCacheMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.locale.LocaleMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "mezzanine.core.request.CurrentRequestMiddleware",

    "mezzanine.core.middleware.TemplateForDeviceMiddleware",
    "mezzanine.core.middleware.TemplateForHostMiddleware",
    "mezzanine.core.middleware.AdminLoginInterfaceSelectorMiddleware",
    "mezzanine.core.middleware.SitePermissionMiddleware",
    "mezzanine.pages.middleware.PageMiddleware",
    "mezzanine.core.middleware.FetchFromCacheMiddleware",
    "mezzanine.core.middleware.RedirectFallbackMiddleware",
)

是的,根文件夹中的urls.py包含admin.autodiscover()。

是的,我使用了manage.py syncdb并重启了服务器,就像一百万次。

如果你发现我没有做的事,请帮助我,因为我从头上撕下了所有的头发。

由于

1 个答案:

答案 0 :(得分:0)

原来问题不在于代码!

命令python manage.py syncdb没有返回我需要的所有内容。使用python manage.py syncdb --allauth_permission_pkey(在PgSql上)的主键上返回了唯一约束错误。

命令python manage.py sqlsequencereset APP_NAME生成要执行的SQL查询以修复序列。

因为我的错误是使用了auth_permission,所以我使用了python manage.py sqlsequencereset auth,这使我可以运行这些查询:

BEGIN;
SELECT setval(pg_get_serial_sequence('"auth_permission"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "auth_permission";
SELECT setval(pg_get_serial_sequence('"auth_group_permissions"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "auth_group_permissions";
SELECT setval(pg_get_serial_sequence('"auth_group"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "auth_group";
SELECT setval(pg_get_serial_sequence('"auth_user_groups"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "auth_user_groups";
SELECT setval(pg_get_serial_sequence('"auth_user_user_permissions"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "auth_user_user_permissions";
SELECT setval(pg_get_serial_sequence('"auth_user"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "auth_user";
COMMIT;

所以是的,问题来自于复制整个项目,而不仅仅是所述项目中的应用程序。