2012-06-06 81 views
0

我有一個事件模型。活動可以有很多'主持人'。但每位演示者可以選擇兩種不同類型的配置文件中的一種。 Profile1和Profile2。我如何允許兩個配置文件進入演示者?Django模型:manytomany與多個對象

這將是100%後端製作。至於說,管理員將選擇「主持人」。 (不知道這件事是否重要)。

class Profile1(models.Model): 
    user = models.ForeignKey(User, null=True, unique=True) 
    first_name = models.CharField(max_length=20, null=True, blank=True) 
    last_name = models.CharField(max_length=20, null=True, blank=True) 
    created = models.DateTimeField(auto_now_add=True) 
    modified = models.DateTimeField(auto_now=True) 
    about = models.TextField(null=True, blank=True) 
    tags = models.ManyToManyField(Tag, null=True, blank=True) 
    country = CountryField() 
    avatar = models.ImageField(upload_to='avatars/users/', null=True, blank=True) 
    score = models.FloatField(default=0.0, null=False, blank=True) 
    organization = models.CharField(max_length=2, choices=organizations) 

class Profile2(models.Model): 
    user = models.ForeignKey(User, null=True, unique=True) 
    first_name = models.CharField(max_length=20, null=True, blank=True) 
    last_name = models.CharField(max_length=20, null=True, blank=True) 
    created = models.DateTimeField(auto_now_add=True) 
    modified = models.DateTimeField(auto_now=True) 
    about = models.TextField(null=True, blank=True) 
    tags = models.ManyToManyField(Tag, null=True, blank=True) 
    country = CountryField() 
    avatar = models.ImageField(upload_to='avatars/users/', null=True, blank=True) 
    score = models.FloatField(default=0.0, null=False, blank=True) 

... 

class Event(models.Model): 
    title = models.CharField(max_length=200) 
    sub_heading = models.CharField(max_length=200) 
    presenters = ManyToManyField(Profile1, Profile2, blank=True, null=True) ? 
    ... 
    # I've also tried: 
    profile1_presenters = models.ManyToManyField(Profile1, null=True, blank=True) 
    profile2_presenters = models.ManyToManyField(Profile2, null=True, blank=True) 
    # is there a better way to accomplish this?... 
+0

也許繼承? – cha0site

+0

@ cha0site喜歡https://docs.djangoproject.com/zh/dev/topics/db/models/#multi-table-inheritance?除了製作一個單獨的'Presenters'類之外,我很難理解我如何完成這項工作。如果是這樣,那麼在管理方面更多的工作......是不是? – Modelesq

回答

3

我想你在這裏有一個設計問題。在我看來,你必須考慮演講者是什麼,演員與「個人資料1」和「個人資料2」有什麼不同。你打算如何處理這些模型?你確定只有兩個配置文件?是否有機會在一段時間之後出現不同的檔案(「檔案3」)?和配置文件4?和配置文件N?

我建議你再考慮一下你的模型及其關係。不要讓這個決定考慮從django admin處理這些模型有多困難/容易。這是另一個問題,我敢打賭,如果你認爲你的模型稍微有點,那麼這個問題稍後就不會成爲問題。不過,我可以給你一些關於如何實現你想要的(或者我希望如此)的建議。一旦你想到如何建立這些關係模型,開始考慮如何在django中編寫模型。這裏有一些問題需要你自己去解答:

你需要一個不同的表(如果你打算使用SQL)每個配置文件? 如果您不能回答這個問題,請嘗試回答這些問題: 1)兩個不同配置文件有什麼區別? 2)是否有多​​個配置文件? 3)每個演示者只有一個配置文件?這個屬性在不久的將來會發生什麼變化?

我不知道你需要什麼,但我認爲最好的選擇是在你的「Presenter」模型中有一個「Profile」模型。可能是這樣的:

class Profile(models.Model): 
    first_profile_field = ... 
    second_profile_field = ... 

# Each presenter have one profile. One profile can "represent" 
# to none or more presenters 
class Presenter(models.Model): 
    first_presenter_field = .... 
    second_presenter_field = .... 
    profile = models.ForeignKey(Profile) 


class Event(models.Model): 
    presenters = models.ManyToManyField(Presenter) 
    .... 

這只是一個想法,我想你可以設計你的模型。這裏有一些鏈接,可以幫助你,一旦你有正確設計的模型,並回答了我提出的問題,您:

https://docs.djangoproject.com/en/dev/topics/db/models/#model-inheritance

https://docs.djangoproject.com/en/dev/misc/design-philosophies/#models

http://www.martinfowler.com/eaaCatalog/activeRecord.html

並與管理工作一旦你決定你的設計將如何:

https://docs.djangoproject.com/en/dev/ref/contrib/admin/

編輯: 如果我沒有錯,配置文件1和2的唯一區別是「組織」字段。我對嗎?所以我建議你合併兩個模型,因爲它們幾乎是一樣的。如果他們有不同的方法,或者你想添加不同的經理或其他,你可以使用django模型的proxy option。例如,您可以這樣做:

class Profile(models.Model): 
    #All the fields you listed above, including the "organization" field 


class GoldenProfile(models.Model): 
    #you can define its own managers 
    objects = GoldenProfileManager() 
    .... 
    class Meta: 
     proxy = True 

class SilverProfile(models.Model): 
    .... 
    class Meta: 
     proxy = True 

這樣,您可以在每個模型中定義不同的方法或具有不同行爲的相同方法。你可以給他們自己的經理,等等。

而且事件類應保持這樣的:

class Event(models.Model): 
    title = models.CharField(max_length=200) 
    sub_heading = models.CharField(max_length=200) 
    presenters = ManyToManyField(Profile, blank=True, null=True) 

希望它能幫助!

+0

首先,我只是想說,非常感謝你的意見和幫助。像你這樣的人使得stackoverflow令人驚歎。我認爲我沒有得到如此有益的迴應。並回答你關於配置文件的問題。用戶可以有1個或另一個,或兩者都有(非常少見但可能)。我可以提供的最佳範例:金色配置文件和銀色配置文件。黃金(profile1)可以訪問更多的內容,並且可以做更多的事情(幾乎像主持人),銀(profile2)可以查看/執行基本的事情。至於未來,將不會有第三個簡介。 – Modelesq

+0

我真的很高興它幫助你!但我想繼續這樣做,所以我還有一個問題:一個配置文件和另一個配置文件之間的唯一區別是它們將具有的權限?你打算怎麼做?如果你仔細想想,你能編輯你的問題並添加個人資料模型嗎?例如,如果你必須製作兩個不同的Django模型,每個模型一個,他們有什麼樣的字段? – marianobianchi

+0

他們有不同的權限,他們代表了一種「狀態」。該網站與兩個配置文件完美配合。只是將這兩個配置文件合併到另一個對象中是非常棘手的。 – Modelesq