我想在2个模型的同一页面上创建数据,所以我使用概念Django admin内联表单。数据无法保存在数据库中。
这是我的models.py
class Vehicle_group(models.Model):
name_group = models.CharField(max_length=40, null=True, blank=True)
description = models.TextField()
manufacture = models.CharField(max_length=40, null=True, blank=True)
def __str__(self):
return self.name_group;
class Vehicle_attribute(models.Model):
vehicle_group = models.ForeignKey(Vehicle_group)
name_attribute = models.CharField(max_length=40, null=True, blank=True)
operation = models.CharField(max_length=40, null=True, blank=True)
payload_meter = models.IntegerField()
payload_limit_low = models.IntegerField()
def __str__(self):
return self.name_attribute;
admin.py
from django.contrib import admin
from .models import Vehicle_group, Vehicle_attribute, Unit_list, Mobiletower
class VehicleAttributeInline(admin.TabularInline):
model = Vehicle_attribute
class VehicleGroupAdmin(admin.ModelAdmin):
inlines = [
VehicleAttributeInline,
]
view.py
def vehicle_group_add(request):
if request.method == "POST":
form = VehicleGroup(request.POST, request.FILES)
if form.is_valid():
post = form.save(commit=False)
post.save()
return redirect('vehicle_group_add.html', pk=post.pk)
else:
form = VehicleGroup()
return render(request,'vehicle_group_add.html', {'form': form}, context_instance= RequestContext(request))
form.py
class VehicleGroup(forms.ModelForm):
class Meta:
model = Vehicle_group
fields = ['name_group','description','manufacture','manufacture_type']
我尝试使用一个proses输入数据保存数据,但无法保存在数据库中。
你帮我解决了这个问题吗?