Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin edit multiple models at once

Tags:

python

django

I am trying to find a way to add/edit two models at once. i.e. :

class Desktop(models.Model):
    #some field...

    specs = models.ForeignKey(Specs)

class Specs(models.Model):
    cpu = models.CharField(max_length=200)
    #and some other fields

When I add a new Desktop, I want to be able to add the Specs at the same time. With the normal Django Admin you will get an + symbol, and you can add the values of the ForeignKey. But when you want to edit the foreignkey while editing the Desktop, you can't do it.

UPDATE! I've added the following:

class ServerInLine(admin.StackedInLine): 
    model = Server 
    extra = 1  
class SpecsManager(admin.ModelAdmin): 
    inlines = [ServerInLine]

This makes me able to add an server when adding Specs. But actually I want to add Specs when I add a new Server. So when I add a new Server or Desktop, I want to add the specs. The specs field in Server and Desktop should then link to the specs filled in.

like image 925
RockIt Avatar asked Oct 22 '13 11:10

RockIt


1 Answers

Try this in your admin:

 class DesktopInline(admin.StackedInline):
    model = Desktop
    extra = 1


class SpecsAdmin(admin.ModelAdmin):
    inlines = [DesktopInline,]
admin.site.register(Specs, SpecsAdmin)

take a look at the docs

like image 86
Armance Avatar answered Sep 30 '22 17:09

Armance