Django向auth_user添加字段

时间:2016-10-14 15:29:17

标签: django authentication django-models

我正在尝试实施上述添加字段'帖子'使用第一个选项扩展到用户模型。

models.py

   from django.contrib.auth.models import User

    class NewUserModel(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE)
        posts = models.IntegerField(default=0)

views.py

def site_users(request):
    site_users = User.objects.all().reverse()
    paginator = Paginator(site_users, 10)
    page = request.GET.get('page')
    try:
        users_model = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        users_model = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        users_model = paginator.page(paginator.num_pages)

return render(request, 'site_users.html', {'users_model': users_model, 'current_time':   timezone.now()})

但是如何在模板中获取帖子字段(更新,更正并正常工作!)

{% for item in users_model %}
        <tr>
          <td><a href="{% url 'profile' item.id %}">{{ item.username }}</a></td>
          <td>{{ item.newusermodel.posts }}</td>
          <td></td>
          <td></td>
        </tr>
        {% endfor %}

sqlite3 DB中的表没有User模型,但它确实有auth_user。我应该使用它而不是用户吗?

enter image description here

谢谢,

1 个答案:

答案 0 :(得分:1)

$state.go('login')未引用users_model,引用NewUserModel。您需要访问模板中的相关模型,如下所示:

User