원하는 공통된 위치 모델을 원한다면 위치를 필요로하는 다양한 고급 모델을 참조해야합니다.Django 관리자 인라인 및 스키마 디자인
게시자와 건물에 대한 더 높은 수준의 정보와 각각에 대한 위치 정보를 입력 할 수있는 여러 부분 형식 (인라인)으로 관리자에게 내 사용자를 표시하려고합니다. 인라인 시스템은 이런 식으로 작동하지 않는 것 같습니다.
분명히, 나에게 매우 표준적인 문제처럼 보이기 때문에 분명히 뭔가 잘못하고 있습니다. 내 스키마 디자인이 골칫거리니까?
나는 바보로 인라인 시스템을 사용합니까? 각각의 상위 레벨 객체에 대한 Location의 하위 클래스를 수행하고 싶지 않습니다. 상위 레벨 객체가 소유하고있는 다른 위치와는 다른 방식으로 위치를 조작하고 싶기 때문입니다 (메일 링리스트 또는 지형 검색).
models.py:
...
class Location(models.Model):
"""
A geographical address
"""
# Standard Location stuff
address_line1 = models.CharField("Address line 1", max_length = 45, null=True, blank=True)
...
class Publisher(models.Model):
"""
Contains Publisher information for publishers of yearbooks. Replaces Institution from 1.x
"""
name = models.CharField(max_length=100, null=False, help_text="Name of publisher, e.g. University of Kansas")
groups = models.ManyToManyField(Group, help_text="Select groups that this publisher owns. Usually just one, but multiple groups are possible.")
is_active = models.BooleanField(help_text="Check this box to enable this publisher.")
location = models.OneToOneField(Location)
...
class Building(models.Model):
"""
Contains Building Information
"""
name = models.CharField(max_length=100, null=False, help_text="Name of building, e.g. Physical Sciences")
is_active = models.BooleanField(help_text="Check this box to enable this building.")
location = models.OneToOneField(Location)
...
admin.py:
...
class LocationInline(generic.GenericStackedInline):
model = Location
max_num = 1
extra = 1
class PublisherAdmin(admin.ModelAdmin):
model = Publisher
inlines = [ LocationInline,
]
class BuildingAdmin(admin.ModelAdmin):
model = Building
inlines = [ LocationInline,
]
admin.site.register(Publisher, PublisherAdmin)
admin.site.register(Building, BuildingAdmin)
내가 인라인 개체를받을 수 있나요 및 편집 할 수 있습니다 경우에도이를 할 때, 관계 거꾸로 보인다
# Support reverse lookup for admin
object_id = models.PositiveIntegerField()
content_type = models.ForeignKey(ContentType)
of = generic.GenericForeignKey('content_type', 'object_id')
:
나는 위치 모델이 추가하여로드 인라인을 강제로 제시 할 수 있습니다 나에게, 그것을 만든 객체에 ID를 저장하는 Location.
모든 것이 훌륭하게 작동하도록 권장되는 스키마 변경 (장고가 너무 뛰어남) 또는 거꾸로 보이는 것들을 이해하기위한 트릭입니다.
그게 제가 그렉을 놓친 것입니다. 어떤 인식을 가진 '부모'객체없이 _itself_를 다른 객체에 붙이는 개념. 내 OneToOne 필드는 그 상황에서 불필요합니다. 알았다. 고맙습니다. –