我希望使用相似的语言对象生成多个国家/地区对象,而无需通过管理界面。
models.py
from django.db import models
class World(models.Model):
country = models.CharField(max_length=200)
Language = models.CharField(max_length=200)
class add_countries(models.Model):
sub_part1 = ['USA','ENGLAND','AUSTRALIA','CANADA']
for sub in sub_part1:
sub = World.country
World.Language = "English"
add_countries()
答案 0 :(得分:1)
addCard = async ({
number, expMonth, expYear, cvc,
}) => {
this.setState({ addingCardInProcess: true });
try {
const tokenObject = await stripe.createTokenWithCard({
number, expMonth, expYear, cvc
});
firebase
.database()
.ref(`/stripe_customers/${uid()}/sources`)
.push({ token: tokenObject.tokenId })
.then(() => {
this.setState({ addingCardInProcess: false });
this.cardAlert(true);
})
.catch((err) => {
this.setState({ addingCardInProcess: false });
this.cardAlert(false, err.message);
});
} catch(err) {
this.cardAlert(false, err.message);
this.setState({ addingCardInProcess: false })
}
};
您可以将上面的代码放在方法中并调用它。
答案 1 :(得分:1)
如果您希望有一些始终存在的数据库条目,您应该考虑使用data migrations。这将导致在迁移数据库时创建这些数据库行。基本的想法是这样的:
from django.db import migrations
def add_languages(apps, schema_editor):
# We can't import the Person model directly as it may be a newer
# version than this migration expects. We use the historical version.
World = apps.get_model('yourappname', 'World')
languages = {'English': ['USA','ENGLAND','AUSTRALIA','CANADA']}
for language, countries in languages.items():
for country in countries:
World.objects.get_or_create(country=country, Language=language)
class Migration(migrations.Migration):
dependencies = [
('yourappname', '0001_initial'),
]
operations = [
migrations.RunPython(add_languages),
]
迁移文件将位于您应用的迁移目录中(在本例中类似于project_root/yourappname/migrations/0002_insert_languages.py
)。有关详细信息,请参阅文档。