模型未显示在管理中
Model not showing in admin
我必须处理一个与另一个项目重复的项目。第一次使用 Python、Django 和 Mezzanine。
我复制了一个应用程序,因为我需要几乎完全相同的东西,但使用另一个模板和一些不同的字段。所以我复制了文件夹,重命名了几乎所有需要从 Events 重命名为 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 并重新启动了服务器,大约一百万次。
拜托,如果你发现我做错了什么,请帮助我,因为我把头发都扯掉了。
谢谢
原来问题不在代码上!
命令 python manage.py syncdb
没有 return 我需要的一切。使用 python manage.py syncdb --all
return 在 auth_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;
是的,问题来自复制整个项目,而不仅仅是所述项目中的应用程序。
我必须处理一个与另一个项目重复的项目。第一次使用 Python、Django 和 Mezzanine。
我复制了一个应用程序,因为我需要几乎完全相同的东西,但使用另一个模板和一些不同的字段。所以我复制了文件夹,重命名了几乎所有需要从 Events 重命名为 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 并重新启动了服务器,大约一百万次。
拜托,如果你发现我做错了什么,请帮助我,因为我把头发都扯掉了。
谢谢
原来问题不在代码上!
命令 python manage.py syncdb
没有 return 我需要的一切。使用 python manage.py syncdb --all
return 在 auth_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;
是的,问题来自复制整个项目,而不仅仅是所述项目中的应用程序。