我有两张如下表,
from django.db import models
class AggregationCategories(models.Model):
name = models.CharField(max_length=50)
class Meta:
db_table = "AggregationCategories"
class AggregationFunctions(models.Model):
name = models.CharField(max_length=50)
aggregation_category = models.ManyToManyField(
AggregationCategories,
related_name='aggregationfunctions_aggregationcategories'
)
class Meta:
db_table = "AggregationFunctions"
当您创建多对多关系时,您将获得额外的表结构<model name>_<filed name>
。所以根据我的例子model name
isAggregationFunctions
和field name
is aggregation_category
。基于此,我的附加表应该是AggregationFunctions_aggregation_category
. 是的,您可以在下图中看到所有三个表格。
因此,您可以像这样将数据转储到第三个表中,python manage.py dumpdata <app name>.<third table name>
. 所以 my app name
iscatalogue
和 the third table name
is AggregationFunctions_aggregation_category
。基于此,我的命令应该是,
python manage.py dumpdata catalogue.AggregationFunctions_aggregation_category
如果你已经使用ManyToManyField.through、ManyToManyField.through_fields创建了多对多表,例如(直接取自 Django 官方文档),
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=50)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(
Person,
through='Membership',
through_fields=('group', 'person'),
)
class Membership(models.Model):
group = models.ForeignKey(Group, on_delete=models.CASCADE)
person = models.ForeignKey(Person, on_delete=models.CASCADE)
inviter = models.ForeignKey(
Person,
on_delete=models.CASCADE,
related_name="membership_invites",
)
invite_reason = models.CharField(max_length=64)
现在你有一个Membership
多对多关系的类调用,所以你可以像以前一样使用它,
python manage.py dumpdata catalogue.Membership