2011-06-11 51 views
0

我已經創建了下面的M2M表中介模型M2M入門 -刪除與中介模式

class Position(models.Model): 
    position = models.CharField(max_length=100) 

class UserProfile(models.Model): 
    user = models.ForeignKey(User, unique=True) 
    positions = models.ManyToManyField(Position, through ='Timestamp', blank=True, null=True) 

class Timestamp(models.Model): 
    created_at = models.DateTimeField(auto_now_add=True) 
    position = models.ForeignKey(Position) 
    userprofile = models.ForeignKey(UserProfile) 

我創建了以下條目至今 -

>>> entry1 = Timestamp(userprofile=userprofile, position=position1) 
>>> entry2 = Timestamp(userprofile=userprofile, position=position2) 

現在我必須在userprofile_timestamp

-- profile & position1 
-- profile & position2 

兩個項目我將如何刪除profile & position1入口?在文檔(https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships)中,它提到使用profile.positions.clear()清除所有條目,但我將如何刪除單個條目?謝謝。

回答

0
>>> profile.positions.through.objects.get(position=position1).delete()