将用户添加到ManyToManyField

时间:2017-09-04 11:08:30

标签: django database django-models

我未能将用户添加到ManyToManyField中。使用Django 1.11。请遵循以下文档:https://docs.djangoproject.com/en/1.11/ref/models/relations/#django.db.models.fields.related.RelatedManager.set

我的模板:

def add_story(request, park_id):

    story = get_object_or_404(Story, pk=story_id)    
    story.users.add(request.user)

    stories = Story.objects.filter(users=request.user)

    context = {
        'stories': stories,
    }
    return render(request, 'story/manage.html', context)

我的观点:

class Story(models.Model):
    story_name = models.CharField(max_length=550)    
    users = models.ManyToManyField(User, related_name='subscriber')
    dateAdded = models.DateTimeField(auto_now_add=True)
    userAdded = models.ForeignKey(User)

我的模特:

{{1}}

我无法弄清楚我哪里出错了。用户未添加,我根本没有错误。

0 个答案:

没有答案