遵循教程后的 m2m django 实现

m2m django implementation after following a tutorial

场景:我有一些盒子(容器)我有一些对象(样本)一个样本可以拆分成很多盒子,一个盒子可以包含很多样本。

我希望能够将一个样本分配给一个盒子并从一个盒子中取出一个样本。

我遵循这些 tutorials 57-59,将朋友分配给用户,并且成功了。

所以我现在尝试调整代码,因此我需要将用户更改为 boxes/containers,将朋友更改为样本。听起来很简单。但是我对 Django 的怪癖没有经验, request.user 在哪里我似乎无法获得正确的语法。所以这里是代码,首先是教程中的代码,然后是我重构它的尝试。

我还有 2 个其他 tables/models 容器和样本,其中 ContainerContent 模型适合它们。

# models.py (tutorial)
class Friend(models.Model):
    users = models.ManyToManyField(User)
    current_user = models.ForeignKey(User, related_name='owner', null=True, on_delete = models.PROTECT)
    # container_id = models.ForeignKey(Container, null=True, on_delete = models.PROTECT)

    @classmethod
    def make_friend(cls, current_user, new_friend):
        friend, created = cls.objects.get_or_create(
            current_user=current_user
        )
        friend.users.add(new_friend)

    @classmethod
    def lose_friend(cls, current_user, new_friend):
        friend, created = cls.objects.get_or_create(
            current_user=current_user
        )
        friend.users.remove(new_friend)


# views.py
def change_friends(request, operation, pk):
    friend = User.objects.get(pk=pk)
    if operation == 'add':
        Friend.make_friend(request.user, friend)
    elif operation == 'remove':
        Friend.lose_friend(request.user, friend)

    return redirect('depot:allcontainer')


#urls.py
url(r'^container/(?P<operation>.*)/(?P<pk>\d+)/$', views.change_friends, name='change_friends'),


#html
...
        <tbody>
          {% for user in users %}
          <tr>
            {% if user not in friends %}
            <!-- we will want to add an if stmt list if not in unassigned - need to think how to do this -->
            <td>{{ container.container_id }}</td>
            <td>{{ user.username }}</td>
            <td>  <a href="{% url 'depot:change_friends' operation='add' pk=user.pk %}"  class="badge badge-primary" role="button">
              <!-- container=container.container_id -->
              <!-- container=container.container_id -->
              <!-- <button type="button" class="btn btn-success">add</button> -->
              >>
            </a></td>
            {% endif %}
          </tr>
          {% endfor %}
        </tbody>
...

...
      <tbody>
      <tr>
          {% for friend in friends %}
          <td><a href="{% url 'depot:change_friends'  operation='remove' pk=friend.pk %}" class="badge badge-primary" role="button">
            <<
          </a></td>
          <td>{{ friend.username }}</td>
        </tr>
        {% endfor %}
      </tbody>
...

以下是我的尝试:

# models.py
class ContainerContents(models.Model):
    sample = models.ManyToManyField('Sample')
    current_container = models.ForeignKey(Container, null=True, on_delete = models.PROTECT)

        @classmethod
        def add_to_container(cls, current_container, new_sample):
            sample, created = cls.objects.get_or_create(
                current_container=current_container
            )
            sample.add(new_sample)

        @classmethod
        def remove_from_container(cls, current_container, new_sample):
            sample, created = cls.objects.get_or_create(
                current_container=current_container
            )
            sample.remove(new_sample)

# views.py - this one is causing me issues, the request.____
def change_container(request, operation, pk, fk='', sample_id=''):
    container = Container.objects.get(pk=pk)
    sample = Sample.objects.get(pk=fk)
    # sample = Container.objects.get(container.sample_id=sample_id)
    if operation == 'add':
        ContainerContents.add_to_container(request.container, container)
    elif operation == 'remove':
        ContainerContents.remove_from_container(request.container, container)

    return redirect('depot:allcontainer')

# urls.py
url(r'^change_container/(?P<operation>.*)/(?P<pk>\d+)/sample/(?P<fk>\d+)$', views.change_container, name='change_container'),

我怀疑我需要在这里传递容器id,否则容器之间不会有任何区别。

# html
    <tbody>
      {% for unassigned in container_contents %}
      <tr>
        <!-- { if user not in friends } -->
        <!-- we will want to add an if stmt list if not in unassigned - need to think how to do this -->
        <td>{{ unassigned.area_easting }}.
          {{ unassigned.area_northing }}.
          {{ unassigned.context_number }}.
          {{ unassigned.sample_number }}</td>
          <td>{{ unassigned.sample_id }}</td>
          <td></td>
          <td>  <a href="{ url 'depot:change_friends' operation='add' pk=user.pk }"  class="badge badge-primary" role="button">
            <!-- container=container.container_id -->
            <!-- container=container.container_id -->
            <!-- <button type="button" class="btn btn-success">add</button> -->
            >>
          </a></td>
          <!-- { endif } -->
        </tr>
        {% endfor %}
      </tbody>
...
...

    <tbody>
      <tr>
        {% for contents in container_contents %}
        <td><a href="{% url 'depot:change_container'  operation='remove' pk=container.container_id fk=contents.sample_id  %}" class="badge badge-primary" role="button">
          <!-- <button type="button" class="btn btn-default">remove</button> -->
          <<
        </a></td>
        <td>{{ contents.sample_id }}</td>
        <td>{{ contents.area_easting }}.
          {{ contents.area_northing }}.
          {{ contents.context_number }}.
          {{ contents.sample_number }}</td>
        </tr>
        {% endfor %}
      </tbody>
...       

---更新---

我应该包含生成页面的视图,而不是 users/friends 仍然包含在其中,一旦我开始工作就会将其删除。

def detailcontainer(request, container_id):
    container = get_object_or_404(Container, pk=container_id)
    samples = container.samples.all()
    # allsamples = container.samples.exclude(sample_id=samples)
    allsamples = container.samples.all()

    users = User.objects.exclude(id=request.user.id).order_by('-id')
    friend = Friend.objects.get(current_user=request.user)
    friends = friend.users.all().order_by('-id')

    container_contents = container.samples.all()
    # container_contents = Container.objects.get(current_container=samples)

    return render(request, 'container/detailcontainer.html',
    {'container':container,
    'samples':samples,
    'allsamples': allsamples,

    'users': users,
    'friends': friends,

    'container_contents': container_contents,

    })

它应该会导致问题,因为 request 没有任何名为容器的属性。在您的教程示例中,它使用 request.user 获得了 logged in user,因为 django 将 logged in user 实例分配给 request(通过中间件)。

由于您的 change_container 视图方法中已经有 samplecontainer 对象,您可以这样尝试:

if operation == 'add':
    ContainerContents.add_to_container(container, sample)
elif operation == 'remove':
    ContainerContents.remove_from_container(container, sample)

更新

遗漏了一件事,你需要在add_to_containerremove_from_container方法中进行更改:

    @classmethod
    def add_to_container(cls, current_container, new_sample):
        container, created = cls.objects.get_or_create(
            current_container=current_container
        )
        container.sample.add(new_sample)

    @classmethod
    def remove_from_container(cls, current_container, new_sample):
        container, created = cls.objects.get_or_create(
            current_container=current_container
        )
        container.sample.remove(new_sample)

因为示例是一个 ManyToMany 字段,在 CurrentContainerSample 模型之间建立连接。

更新 2

@classmethod
def remove_from_container(cls, current_container, new_sample):
     from app_name.models import ContainerSample

     c_sample = ContainerSample.objects.get(container=current_container, sample=new_sample)
     c_sample.delete()

您没有在获取的对象中引用您的 m2m 字段。您需要按以下方式处理 sample 字段:

models.py:

@classmethod
def add_to_container(cls, current_container, new_sample):
    containerContents, created = cls.objects.get_or_create(
        current_container=current_container
    )
    containerContents.sample.add(new_sample)

@classmethod
def remove_from_container(cls, current_container, new_sample):
    containerContents, created = cls.objects.get_or_create(
        current_container=current_container
    )
    containerContents.sample.remove(new_sample)

并为您的模型方法设置适当的变量:

views.py

def change_container(request, operation, pk, fk='', sample_id=''):
    container = Container.objects.get(pk=pk)
    sample = Sample.objects.get(pk=fk)
    # sample = Container.objects.get(container.sample_id=sample_id)
    if operation == 'add':
        ContainerContents.add_to_container(container, sample)
    elif operation == 'remove':
        ContainerContents.remove_from_container(container, sample)

    return redirect('depot:allcontainer')