D
D
Denis Michurin2017-11-24 14:03:13
PHP
Denis Michurin, 2017-11-24 14:03:13

How to display values ​​from 2 models in Django?

Good afternoon, I am writing a small note for inventory.
Created 2 models, linked them using ForeignKey

models.py

from django.db import models

# Create your models here.

class Employees(models.Model):
    user = models.CharField('Сотрудник:', max_length=300)
    email = models.EmailField('Email сотрудника:', default=None)
    is_active_status = models.BooleanField('Активен',default=True,)

    def __str__(self):
        return '%s' % self.user

    class Meta:
        verbose_name = 'Сотрудник'
        verbose_name_plural = 'Сотрудники'


class Equipment(models.Model):
    inventory_number = models.IntegerField('Инвентарный номер:')
    user = models.ForeignKey('Employees', name='Сотрудник:', blank=True, null=True, default="Склад")
    name = models.TextField('Оборудование:',max_length=300)
    is_active = models.BooleanField('Активен:',default=True,)

    def __str__(self):
        return '%s' % self.name

    class Meta:
        verbose_name = 'Оборудование'
        verbose_name_plural = 'Обрудование'


I set up the display I need in the admin panel
admin.py

from django.contrib import admin
from .models import *


class EqmAdminInline(admin.TabularInline):
    model = Equipment
    extra = 1

class UserAdmin(admin.ModelAdmin):
    list_display = [ field.name for field in Employees._meta.fields]
    search_fields = [ search.name for search in Employees._meta.fields]
    inlines = [EqmAdminInline]

    class Meta:
        model = Employees


admin.site.register(Employees, UserAdmin)


class EqmAdmin(admin.ModelAdmin):
    list_display = [ field.name for field in Equipment._meta.fields]
    search_fields = [search.name for search in Equipment._meta.fields]


    class Meta:
        model = Equipment

admin.site.register(Equipment,EqmAdmin)


And I process it
views.py
from .models import *

@login_required()
def management_views(request):
    equipment = Equipment.objects.all()
    return render(request,'panel.html', locals())

panel.html

<div class="container tabl">
        <div class="row ">
            <div class="col-xs-0 col-md-2"></div>
            <div class="col-xs-0 col-md-1">Инв.номер:</div>
            <div class="col-xs-0 col-md-3">Оборудование:</div>
            <div class="col-xs-0 col-md-3">Сотрудник:</div>
            <div class="col-xs-0 col-md-1">Статус:</div>
            <div class="col-xs-0 col-md-2"></div>
        </div>
        {% for management in equipment%}
            <div class="row ">
                <div class="col-xs-0 col-md-2"></div>
                <div class="col-xs-0 col-md-1">{{ management.inventory_number }}</div>
                <div class="col-xs-0 col-md-3">{{ management.name }}</div>
                <div class="col-xs-0 col-md-3">{{ management.user }}</div>
                <div class="col-xs-0 col-md-1">{{ management.is_active }}</div>
                <div class="col-xs-0 col-md-2"></div>
            </div>
        {% endfor %}
        </div>
    </div>
{% endblock %}


So, according to the idea, when displaying management.user, it should display the name of the employee from the Employees database, but this does not happen. Tell me how to get it right

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2019-09-06
@Cat_usual

R::getAssocRow('SELECT clicks, id, login FROM users ORDER BY clicks');

A
Alexey Sergeev, 2017-11-24
@denistu10

{{ management.user.user}}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question