2013-07-23 36 views
2

models.py創建日期排在創建超級用戶

TITLE = (
    ('Classroom', 'Classroom'), 
    ('Playground', 'Playground'), 
    ('Staff Room','Staff Room'), 
) 

class Location(models.Model): 
    user = models.ForeignKey(User,null=True) 
    title = models.CharField('Incident Type', max_length=200,default=TITLE) 
    parent_location_id = models.CharField('Parent Location', max_length=100, null=True, blank=True) 
    is_active = models.BooleanField('Is Active', default=True) 

def location_title(sender, instance, created, **kwargs):   
    if instance.is_superuser and not instance.location.is_active: 

     instance.location.is_active=True 
     instance.location.save() 

post_save.connect(location_title, sender=User) 

我想默認的數據插入到具有一定conditions.This同時通過manage.py createsuperuser評論創建超級用戶應該發生數據庫。

我不知道這是可能與Django,但它的要求。我試着用上面的代碼。我得到的錯誤「AttributeError:'用戶'對象沒有屬性'位置' 」,同時創建超級用戶。

什麼需要我下面

Sample output

+0

有沒有什麼機會可以實現這一點。 – user2086641

+0

這是什麼django版本? – mariodev

+0

我正在使用Django 1.3.7 – user2086641

回答

4

給出試試這個功能信號處理程序的示例:

def location_title(sender, instance, created, **kwargs): 
    # Don't fire up on updates. 
    if not created: 
     return 

    # Only handle new superusers. 
    if not instance.is_superuser or not instance.is_active: 
     return 

    # Create a `Location` entry for new superuser. 
    l = Location(user_id=instance.pk) 
    l.save() 

post_save.connect(location_title, sender=User) 

添加的選擇,模型字段:

Django的CharField有一個名爲參數choices,它允許你給最終用戶一個可能的值列表,並對它們進行適當的驗證形式。可迭代的格式如下<internal_value>, <display_value>。一旦一個字段通過了choices參數,就可以通過instance.get_<field_name>_display()方法訪問連接到其內部值的顯示值。

的選擇迭代看起來是這樣的:

class Location(models.Model): 
    class Title: 
     CLASSROOM = 'classroom' 
     PLAYGROUND = 'playground' 
     STAFF_ROOM = 'staff_room' 

    TITLE_CHOICES = (
     (Title.CLASSROOM, 'Classroom'), 
     (Title.PLAYGROUND, 'Playground'), 
     (Title.STAFF_ROOM, 'Staff Room'), 
    ) 

    user = models.ForeignKey(User,null=True) 
    title = models.CharField('Incident Type', max_length=200,choices=TITLE_CHOICES,default=Title.CLASSROOM) 
    parent_location_id = models.CharField('Parent Location', max_length=100, null=True, blank=True) 
    is_active = models.BooleanField('Is Active', default=True) 

最終的解決方案是:

class Location(models.Model): 
    class Title: 
     CLASSROOM = 'classroom' 
     PLAYGROUND = 'playground' 
     STAFF_ROOM = 'staff_room' 

    BASE_LOCATION = Title.CLASSROOM 

    TITLE_CHOICES = (
     (Title.CLASSROOM, 'Classroom'), 
     (Title.PLAYGROUND, 'Playground'), 
     (Title.STAFF_ROOM, 'Staff Room'), 
    ) 

    user = models.ForeignKey(User,null=True) 
    title = models.CharField('Incident Type', max_length=200,choices=TITLE_CHOICES,default=Title.CLASSROOM) 
    parent_location_id = models.CharField('Parent Location', max_length=100, null=True, blank=True) 
    is_active = models.BooleanField('Is Active', default=True) 


def location_title(sender, instance, created, **kwargs): 
    # Don't fire up on updates. 
    if not created: 
     return 

    # Only handle new superusers. 
    if not instance.is_superuser or not instance.is_active: 
     return 

    # Create a `Location` entry for new superuser. 
    base = Location(user_id=instance.pk, title=Location.BASE_LOCATION) 
    base.save() 

    for value, _ in Location.TITLE_CHOICES: 
     if value == Location.BASE_LOCATION: 
      continue 

     l = Location(user_id=instance.pk, title=value, parent_location_id=base.pk) 
     l.save() 

post_save.connect(location_title, sender=User) 
+0

上面的代碼是在位置table.I創建一個行我試圖包括在標題領域的選擇,所以我必須在數據庫中創建一個默認的數據。我想要的輸出是在示例圖片給出的。可以指導我如何要做到這一點。 – user2086641

+0

如果您想在標題字段中有「選擇」,請使用值爲「TITLE」的命名參數'choices'。要設置默認值,請使用'TITLE'元組的元素,即TITLE [0] [0]將'Classroom'設置爲'default'命名參數的默認值。選擇元組是元素''的元組,其中'internal_value'應該是內部使用的值 - 在腳本,db等中,而'display_value'是顯示給前端用戶的值。 –

+0

剛纔我檢查了你的評論,它看起來像一個不同的方法,你可以解釋我如何做到這一點。 – user2086641

3

當你創建模型實例被創建的用戶或超級用戶,但它並沒有對應location行。因此,訪問instance.location.is_active會導致錯誤。

您可以更新信號處理程序以首先創建location實例,然後設置適當的屬性。如下圖所示:

def location_title(sender, instance, created, **kwargs):  
    #also check for created flag 
    if created and instance.is_superuser: 
     location = Location(user=instance) 
     location.is_active=True 
     location.save() 

post_save.connect(location_title, sender=User) 

如果你想爲title場選擇,你可以定義的字段。您的title領域的定義是不正確的,它改變

title = models.CharField('Incident Type', max_length=200, choices=TITLE, 
         default='Classroom') 

您正確使用detfault=TITLE而不是choices=TITLE來。

+0

如果已創建,而不是instance.is_superuser: - >在數據庫中創建了一行數據。我想將問題中提到的選項包含在標題字段中。如何將其包含在標題字段中。 – user2086641

+0

@ user2086641,你也可以設置'location.title ='Classroom''。請注意,'Location'與'User'具有'OneToOne'關係,因此一個用戶不能有多個位置對象(標題不同)。 – Rohan

+0

可以用與用戶表的ForeignKey關係。 – user2086641