简体   繁体   English

基于另一个模型的实例自动创建模型的实例

[英]Create instance of a model automatically based on instance of another model

I want to create 4 instances of tyres and 1 instance of engine when I create instance of car model here is my model code我想在创建汽车模型实例时创建 4 个轮胎实例和 1 个引擎实例这是我的模型代码

class Car(models.Model):
    company=models.CharField(max_length=25)
    car_model = models.CharField(max_length = 25)
    year = models.IntegerField()

class Engine(models.Model):
    car=models.OneToOneField(Car)
    engine_type=models.CharField(max_length = 15)
    no_cylinders=models.IntegerField()
    state=models.CharField(max_length = 15)
class Tyre(models.Model):  
    car=models.ForeignKey(Car)
    tyre_material=models.CharField(max_length = 15)
    position=models.CharField(max_length = 10)  #ex front right back left 
    state=models.CharField(max_length = 15)

and the form for car和汽车的形式

class car_form(forms.ModelForm):
    
    class Meta:
        model = Car
        fields = (
            'comapany',
            'car_model',
            'year'
            )

    def save(self, commit=True):
        obj=car_form.save()
        obj.company = self.cleaned_data['comapany']
        obj.car_model = self.cleaned_data['car_model']
        obj.year = self.cleaned_data['year']
        
        if commit:
            obj.save()

    return obj

and here is my view这是我的看法

def car_create(request):
    if request.method =='POST':
        form = car_form(request.POST)
        if form.is_valid():
            form.save()
            return redirect("/")
        else :
            return render(request, 'agency/car_form.html', 
                          {'form': form})
    else:
        form = car_form()

        args = {'form': form}
        return render(request, 'agency/car_form.html', args)

What should I change in my code?我应该在我的代码中更改什么?

You could create your instances based on your parent instance like : 您可以根据父实例创建实例,例如:

Views.py Views.py

...
    new_car = form.save()
    engine = Engine.objects.create(car=new_car)
    for i in range(4):
        tyre = Tyre.objects.create(car=new_car)
...

最好的办法是使用信号

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于另一个 model 的实例在 Django 中创建 model,但经过过滤 - How to create a model in Django based on the instance of another model, but filtered 在创建另一个Django模型实例的同时创建一个Django模型实例 - Create a Django model instance concurrent with the creation of another Django model instance 如何基于Django中的属性值将模型实例复制到另一个模型 - How to copy a model instance to another model based on attribute value in Django 如何创建一个 model 实例 - How to create an a model instance 在Django中从另一个模型的实例创建模型实例,而无需链接到原始实例源 - Create model instance from another model's instance in Django without link to original instance source Django:如何在每次更新另一个模型实例时自动更新模型实例 - Django: How to automatically update a model instance everytime another model instance is updated 创建另一个模型的实例时自动填充Django模型字段 - Autofill Django model fields when create instance of another model Django Rest 框架 - 每次创建新的 model B 实例时自动创建新的 Z20F35E630DAF44D9FACZ3C 实例 - Django Rest Framework - Automatically create new model B instance each and every time new model A instance is created 如何将模型的实例传递给 Django 中的另一个模型? - How to pass instance of a model to another model in Django? 将Django模型实例保存到另一个模型中 - Saving django model instance into another model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM