Django模型设计用于基本库存应用程序

时间:2016-04-18 02:12:13

标签: django

我是Django的新手(以及此类数据库),并尝试创建一个简单的库存应用程序来帮助学习。我已经完成了这些教程,并且正在阅读一些书籍,但我仍然坚持我认为很简单,不知道在哪里看或如何提问。

使用库存应用程序,您的设备随后有一个制造商,该设备的型号仅为该制造商所具有的。让我们说戴尔Optiplex 3040.我现在也在使用管理控制台。所以我希望能够将设备与制造商联系起来,然后将设备与型号联系起来。这似乎是因为我需要使用多对多的字段和直通字段来完成我想要做的事情,但我不认为这是正确的方法(如下面的链接所示)。 https://docs.djangoproject.com/en/1.9/topics/db/models/#many-to-many-relationships

以下是我到目前为止的代码。谢谢。

from django.db import models

# Create your models here.



class Department(models.Model):
    department = models.CharField(max_length=50)
    def __str__(self):
        return self.department

class Manufacturer(models.Model):
    manufacturer = models.CharField(max_length=50)
    def __str__(self):
        return self.manufacturer

class EquipmentModel(models.Model):
    equipmentModel = models.CharField(max_length=50)
    def __str__(self):
        return self.equipmentModel




class Employees(models.Model):
    employee_name_first = models.CharField(max_length=25)
    employee_name_last = models.CharField(max_length=25)
    employee_username = models.CharField(max_length=20)
    phone =  models.IntegerField()
    assigned_equipment = models.ForeignKey('Device', default='undefined')
    department = models.ForeignKey('Department', on_delete=models.CASCADE, default='undefined')
    job_title = models.ManyToManyField('Job_Positions', default='undefined')
    def __str__(self):
        return self.employee_username

class Device(models.Model):
    ip = models.GenericIPAddressField(protocol='IPv4',unpack_ipv4=False,null=True, blank=True)#might be good to seperate IP in its own class because a device can have multiple IP's
    department = models.ForeignKey('Department', on_delete=models.CASCADE)
    manufacturer = models.ForeignKey('Manufacturer', on_delete=models.CASCADE)
    serial_number = models.CharField(max_length=50)
    date_created = models.DateTimeField(auto_now=False, auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    comments = models.TextField(blank=True)


    def __str__(self):
        return self.serial_number


class Job_Positions(models.Model):
    position_title = models.CharField(max_length=50)
    position_description = models.TextField(blank=True)
    def __str__(self):
        return position_title

***编辑以添加更新的代码和admin.py代码以回答我必须回答的问题。

#admin.py
from django.contrib import admin

# Register your models here.


from .models import Device,Department,Manufacturer,Employees, Job_Positions, EquipmentModel

class DeviceModelAdmin(admin.ModelAdmin):
    list_display = ["ip", "department","model","serial_number","date_updated"]
    list_filter = ["department","model","ip"]
    search_fields = ["ip"]
    class Meta:
        model = Device


class EmployeesModelAdmin(admin.ModelAdmin):
    list_display = ["employee_name_first", "employee_name_last", "employee_username", "phone"]
    list_filter = ["department"]
    class Meta:
        model = Employees


admin.site.register(Device, DeviceModelAdmin)
admin.site.register(Department)
admin.site.register(Manufacturer)
admin.site.register(EquipmentModel)
admin.site.register(Employees, EmployeesModelAdmin)
admin.site.register(Job_Positions)

更新了models.py

from django.db import models

# Create your models here.



class Department(models.Model):
    department = models.CharField(max_length=50)
    def __str__(self):
        return self.department

class Manufacturer(models.Model):
    manufacturer = models.CharField(max_length=50)
    def __str__(self):
        return self.manufacturer

class EquipmentModel(models.Model):
    model_number = models.CharField(max_length=50)
    manufacturer = models.ForeignKey('Manufacturer', on_delete=models.CASCADE)
    def __str__(self):
        return self.model_number




class Employees(models.Model):
    employee_name_first = models.CharField(max_length=25)
    employee_name_last = models.CharField(max_length=25)
    employee_username = models.CharField(max_length=20)
    phone =  models.IntegerField()
    assigned_equipment = models.ForeignKey('Device', default='undefined')
    department = models.ForeignKey('Department', on_delete=models.CASCADE, default='undefined')
    job_title = models.ManyToManyField('Job_Positions', default='undefined')
    def __str__(self):
        return self.employee_username

class Device(models.Model):
    ip = models.GenericIPAddressField(protocol='IPv4',unpack_ipv4=False,null=True, blank=True)#might be good to seperate IP in its own class because a device can have multiple IP's
    department = models.ForeignKey('Department', on_delete=models.CASCADE)
    model = models.ForeignKey('EquipmentModel', on_delete=models.CASCADE,null=True)
    serial_number = models.CharField(max_length=50)
    date_created = models.DateTimeField(auto_now=False, auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    comments = models.TextField(blank=True)


    def __str__(self):
        return self.serial_number


class Job_Positions(models.Model):
    position_title = models.CharField(max_length=50)
    position_description = models.TextField(blank=True)
    def __str__(self):
        return position_title

1 个答案:

答案 0 :(得分:0)

多对多的关系不是你想要的,因为任何设备(我假设)只能有一个制造商。

您确实需要一个存储模型信息的中间模型,并且您的EquipmentModel中已有一个模型信息。我建议修改如下:

class EquipmentModel(models.Model):
    # This stores information about a particular model of device
    manufacturer = models.ForeignKey('Manufacturer', on_delete=models.CASCADE)
    model_number = models.CharField(max_length=50)

然后,不要在Device中使用外键给制造商,而是将其替换为设备型号的外键:

class Device(models.Model):
   # ...
   model = models.ForeignKey('EquipmentModel', on_delete=models.CASCADE)