저는 장고를 처음 사용하고 학습 응용 프로그램으로 비용 기록 응용 프로그램을 구축하고 있습니다. 내 모델에서하나의 모델 인스턴스에서 다른 모델 인스턴스로 ManyToMany 필드 복사
나는 다음과 같이 세 가지 클래스가 (나는 약간 간결함을 위해 그들을 간체) :
class AbstractExpense(models.Model):
description = models.CharField(max_length=100)
amount = models.IntegerField()
category = models.ForeignKey('Category')
tags = models.ManyToManyField('Tag')
insert_date = models.DateTimeField(auto_now=True)
class Meta(object):
abstract = True
class Expense(AbstractExpense):
date = models.DateField('Date')
class RecurringExpense(AbstractExpense):
FREQUENCY_CHOICES = (('D', 'daily'),
('W', 'weekly'),
('M', 'monthly'),
('Y', 'yearly'))
start_date = models.DateField(blank=False)
end_date = models.DateField(blank=True, null=True)
last_check = models.DateField(blank=True, null=True)
frequency = models.CharField(blank=False, max_length=1, choices=FREQUENCY_CHOICES)
RecurringExpense
Expense
클래스의 새 인스턴스에 복사해야합니다. 다음 작업을 담당하는
RecurringExpense
방법에서 관련 비트는 다음과 같습니다
완벽하게
Expense(description=self.description,
amount=self.amount,
category=self.category,
# tags=self.tags,
date=expense_date).save()
위의 작품,하지만 난 tags=self.tags
행의 주석을 해제하는 경우, 장고는 불평하고 다음과 같은 오류 던져 :
Exception Type: TypeError
Exception Value: 'tags' is an invalid keyword argument for this function
Exception Location: <snip>/django/db/models/base.py in __init__, line 367
I을 이 문제를 해결하려면 I could create a loop을 알고 있지만 한 번에 같은 작업을 수행 할 수있는보다 우아한 방법이 있는지 궁금합니다.
또한 Expense (...)'/'e.save()'를'Expense.objects.create (...) '로 대체 할 수 있습니다 –
많은 수의 태그가있는 경우 작동하지 않을 수 있습니다 (SQL 드라이버 매달린). 이 경우 모든 태그에 대해 큰 덩어리로 반복하여 추가 할 수 있습니다. – odedfos