Answer the question
In order to leave comments, you need to log in
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
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 = 'Обрудование'
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)
from .models import *
@login_required()
def management_views(request):
equipment = Equipment.objects.all()
return render(request,'panel.html', locals())
<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 %}
Answer the question
In order to leave comments, you need to log in
R::getAssocRow('SELECT clicks, id, login FROM users ORDER BY clicks');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question