Dear Django Coders…
I’am a newbie at Django Coding…
i need help with the Django Admin - Add item Function…
i have this Model:
class MachineGroups(models.Model):
group = models.CharField(verbose_name="Maschinengruppe", max_length=200, unique=True)
added_date = models.DateTimeField("hinzugefügt am", auto_now_add=True)
edited_date = models.DateTimeField("bearbeitet am", auto_now=True)
def __str__(self):
return self.group
class Meta:
verbose_name = "Maschinengruppe"
verbose_name_plural = "Maschinengruppen"
class Machines(models.Model):
name = models.CharField(verbose_name="Maschinenname", max_length=200, unique=True)
group = models.ForeignKey(MachineGroups, verbose_name="Maschinengruppe", on_delete=models.PROTECT)
added_date = models.DateTimeField("hinzugefügt am", auto_now_add=True)
edited_date = models.DateTimeField("bearbeitet am", auto_now=True)
def __str__(self):
return self.name
class Meta:
verbose_name = "Maschine"
verbose_name_plural = "Maschinen"
class MachineModules(models.Model):
machine_module = models.CharField(verbose_name="Maschinenmodul", max_length=200, unique=True)
machine_group = models.ForeignKey(MachineGroups, verbose_name="Maschinengruppe", on_delete=models.PROTECT)
machine_name = models.ForeignKey(Machines, verbose_name="Maschine", on_delete=models.PROTECT)
added_date = models.DateTimeField("hinzugefügt am", auto_now_add=True)
edited_date = models.DateTimeField("bearbeitet am", auto_now=True)
def __str__(self):
return self.name
class Meta:
verbose_name = "Maschinenmodul"
verbose_name_plural = "Maschinenmodule"
and this admin.py:
class MachinesAdmin(admin.ModelAdmin):
fieldsets = [
("Maschinendaten", {"fields": ["name", "group"]}),
]
list_display = ["name", "group", "added_date", "edited_date"]
list_filter = ["group"]
search_fields = ["name", "added_date", "edited_date"]
class MachineGroupsAdmin(admin.ModelAdmin):
fieldsets = [
("Maschinendaten", {"fields": ["group"]}),
]
list_display = ["group", "added_date", "edited_date"]
search_fields = ["group", "added_date", "edited_date"]
class MachineModulesAdmin(admin.ModelAdmin):
fieldsets = [
("Maschinendaten", {"fields": ["machine_name", "machine_module"]}),
]
list_display = ["machine_group", "added_date", "edited_date"]
search_fields = ["machine_group", "added_date", "edited_date"]
I can add items to Machines and MachineGroups normally. The MachineGroup is an select Field. That is fine.
Now i have implemented the MachineModules model.
If i want to add an MachineModel the Machine Field is an select field too. So far so good.
The only thing that i need is that the machine_group column gets the value from the assigned group from Machine because i don’t want to select the Group a second time.
If i add an MachineModule now i get the following Error:
(1048, "Column 'machine_group_id' cannot be null")
I know that the error is shown because an ForeignKey can not be null. But how can i assign the value from the group from the selected Machine to this field?
Can anyone help me please?
Many thanks in advance