开发者

Removing a M2M entry with an intermediary model

I have created the following M2M table with an intermediary model --

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(UserProfil开发者_开发问答e)

I have created the following entries so far --

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

Now I have two entries in the userprofile_timestamp table

-- profile & position1
-- profile & position2

How would I delete the profile & position1 entry? In the docs (https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships) it mentions using profile.positions.clear() to clear ALL the entries, but how would I remove a single entry? Thank you.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜