我正在尝试以某种格式创建YAML文件。已经尝试过正式的Django论坛。但与此同时,我觉得有更多的人可以提供帮助: - )
from django.db import models
# Create your models here.
# class NameManager(models.Manager):
# def get_by_natural_key(self, name):
# return self.get(name=name)
class FactoryName(models.Model):
name = models.CharField(max_length=100, unique=True)
def __str__(self):
return self.name
class Option(models.Model):
name = models.CharField(max_length=100, unique=True)
factory = models.ForeignKey(FactoryName, on_delete=models.CASCADE)
def __str__(self):
return self.name
class AbstractOptionPackage(models.Model):
name = models.CharField(max_length=100, unique=True)
description = models.TextField()
options = models.ManyToManyField(Option)
class Meta:
abstract = True
def __str__(self):
return self.name
class InteriorPackage(AbstractOptionPackage):
def __str__(self):
return self.name
class ExteriorColor(models.Model):
name = models.CharField(max_length=100, unique=True)
def __str__(self):
return self.name
class Export(models.Model):
name = models.CharField(max_length=100, unique=True)
exterior_colors = models.ManyToManyField(ExteriorColor)
interior_package = models.ForeignKey(InteriorPackage, on_delete=models.CASCADE)
def __str__(self):
return self.name
现在我想创建一个看起来大致相同的YAML文件(它将在其他软件中处理,“#...”只是这篇帖子中的评论):
Filename: {{Export.name}}.yaml # I think I already found "open(...)" example code also for serialization.
{{Factory[0].name}}: # Name of first factory
- {{ options[0] from any class derived from AbstractOptionPackage where Option.factory = Factory[0].name }}
- {{ options[1] from any class derived from AbstractOptionPackage where Option.factory = Factory[0].name }}
- [...]
{{Factory[1].name}}: # Name of second factory
- {{ options[0] from any class derived from AbstractOptionPackage where Option.factory = Factory[1].name }}
- {{ options[1] from any class derived from AbstractOptionPackage where Option.factory = Factory[1].name }}
- [...]
Colors:
{{ Export.exterior_colors }}:
- selected_exterior_colors[0]
- selected_exterior_colors[1]
- [...]
# to be extended with interior_colors.
或作为示例(应该是非常有效的YAML):
文件名:vehicle_model_A.yaml
Factory_A:
- basic_air_conditioning
- more_chrome
Factory_B:
- leather_seats
- best_radio
Colors:
exterior_colors:
- green
- yellow
我已经可以通过浏览器创建某种YAML文件(PyYAML已安装:-)): admin.py
from django.contrib import admin
from django.http import HttpResponse
from django.core import serializers
# Register your models here.
from .models import Export, FactoryName, Option, InteriorPackage, ExteriorColor
# admin.site.register(Export)
admin.site.register(FactoryName)
admin.site.register(Option)
admin.site.register(InteriorPackage)
admin.site.register(ExteriorColor)
def export_all_as_yaml(modeladmin, request, queryset):
response = HttpResponse(content_type="application/yaml")
//[...]
serializers.serialize("yaml", to_export, stream=response)
return response
class ExportFunction(admin.ModelAdmin):
def exterior_colors_list(self, obj):
return "\n".join([o.name for o in obj.exterior_colors.all()])
list_display = [
'name',
'exterior_colors_list',
'interior_package',
]
ordering = ['name']
actions = [export_all_as_yaml]
admin.site.register(Export, ExportFunction)
但现在有问题了。我在互联网上搜索了很多,但不知何故它没有用。