L
L
leo97972019-08-16 14:56:01
Django
leo9797, 2019-08-16 14:56:01

How to automatically create a model in Django?

For example, I have two models

#Room apps
class Room(models.Model):
    name_room = models.CharField(max_length=20)

    def __str__(self):
        return self.name_room

#Furniture apps

class Furniture(models.Model):
    name_furniture  = models.CharField(max_length=20)
    about_furniture = models.TextField(max_length=156)
    image_furniture = models.ImageField(upload_to='room_media')

    def __str__(self):
        return self.name_furniture

How can I, when creating fields in the Room model, automatically create a name_room name model in the Furniture application in which I can add fields according to the Furniture model?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Astrohas, 2019-08-16
@leo9797

room = models.ForeignKey(Room, on_delete=models.CASCADE)

if one fitting can be in one room, then so be it.
And if you are talking about the admin panel, then use https://docs.djangoproject.com/en/2.2/ref/contrib/...
If you mean automatic creation of fittings after creating a room, then override save models or use signals

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question