如何迭代数据库django诱人语言中的每个用户

时间:2017-08-15 17:17:24

标签: django django-models

当您注册我正在构建的网站时,您会选择是要成为“用户”还是“工作人员”并输入您的位置。对于工作人员,我想显示所有试图在与工作人员相同的位置使用该服务(“用户”)的人。我有以下型号:

models.py:

public class GenericControllerAttribute : Attribute, IControllerModelConvention
{
    public void Apply(ControllerModel controller)
    {
        Type entityType = controller.ControllerType.GetGenericArguments()[0];

        controller.ControllerName = entityType.Name;
    }
}

我还有一个匹配字段的表单,它工作正常,一切都正确保存。

forms.py:

[GenericController]
public class Controller<T> : Controller
{
}

我有一个from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save from django.dispatch import receiver class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) location = models.CharField(max_length=30, blank=True) birth_date = models.DateField(null=True, blank=True) ROLE = ( ('USER', 'User'), # (value to be set on model, human readable value) ('WORKER', 'Worker'), ) role = models.CharField(max_length = 7, choices = ROLE, default = 'USER') def __str__(self): return self.user.username @receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) @receiver(post_save, sender=User) def save_user_profile(sender, instance, **kwargs): instance.profile.save() 视图:

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

class SignUpForm(UserCreationForm):

    birth_date = forms.DateField(help_text='Required. Format: YYYY-MM-DD')
    location = forms.CharField()

    ROLE = (
        ('USER', 'User'),  # (value to be set on model, human readable value)
        ('WORKER', 'Worker'),
    )

    role = forms.ChoiceField(choices=ROLE, required = True)


    class Meta:
        model = User
        fields = ('username', 'first_name','last_name', 'birth_date','location', 'password1', 'password2', )

还有一个home.html,根据帐户是用户还是工作人员,它会显示不同的内容:

home

如何向工作人员帐户显示共享相同def home(request): return render(request, 'core/home.html', {'user': request.user}) 的所有{% extends 'core/base.html' %} {% block head %} <title> Home</title> {% endblock %} {% block body %} {% if user.profile.role == 'USER' %} <h1>User Account</h1> {% elif user.profile.role == 'WORKER' %} <h1>Worker Account</h1> {% for userprofile in user.profile %} {% if userprofile.role == "USER" and userprofile.location == (insert location logic here) %} <h1>{{ userprofile.username }}</h1> {% endif %} {% endfor %} {% else %} <h1>Error</h1> {% endif %} {% endblock %} 个受访者的列表。我不确定home.html中的内容是否正确,但我认为我应该接近这个主题。

1 个答案:

答案 0 :(得分:0)

您应该在视图中执行此操作,然后将其发送到您的html:

def home(request):
    if request.user.profile.role == 'WORKER':
        workers_with_same_location = Profile.objects.filter(location=request.user.location, role='WORKER').exclude(id=request.user.id)
        return render(request, 'core/home.html', {'user': request.user, 'other_workers': workers_with_same_location})
    else:
        render(request, 'core/home.html', {'user': request.user})

然后,在你的html中:

{% if user.profile.role == 'USER' %}
    <h1>User Account</h1>

{% elif user.profile.role == 'WORKER' %}
    <h1>Worker Account</h1>
    {% for worker in other_workers %}
            <h1>{{ worker.user.username }}</h1>
    {% endfor %}

{% else %}
<h1>Error</h1>
{% endif %}

希望它有所帮助!