【问题标题】:Django create data with OnetoOneFieldDjango 使用 OnetoOneField 创建数据
【发布时间】:2020-07-15 13:10:57
【问题描述】:

您好,感谢您的宝贵时间:

我正在尝试通过 management/commands/create_data.py 将一些数据添加到我的模型表中:

问题

除此之外,我想我会在 People 模型的用户字段中收到一个错误,因为应该是一个 id。 我如何获得身份证?

宠物/模型.py:


User = get_user_model()



class People(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='person')
    birthday = models.DateField()
    cpf = models.CharField(max_length=11, validators=[RegexValidator(r'^\d{1,10}$')])

    def __str__(self):
        return '%s' % (self.user)

pets/management/commands/create_data.py:

User = get_user_model()


class GenerateUser():
    username = None
    password = None
    def __init__(self, username, password):
        with open('User.txt') as file:
            for rows in file:
                self.username = slugify(rows)
                self.password = '12345'


class GeneratePeople():
    user = None
    documento = None
    birthday = None
    def __init__(self, documento, birthday):
        with open('People.txt') as file:
            for rows in file:
                t1 = rows.split(', ')
                self.user = slugify(t1[0])
                self.documento = t1[1]
                self.birthday = t1[2]



class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument(
            'file_name', type=str, help='ok')

    def handle(self, *args, **kwargs):

        username = GenerateUser.username
        password = GenerateUser.password
        user = GeneratePeople.user
        documento = GeneratePeople.documento
        birthday = GeneratePeople.birthday

        one = User(
            username=username,
            password=password
            )

        one.save()

        peop = People(
            user=user,
            documento=documento,
            birthday=birthday
        )

        peop.save()


        self.stdout.write(self.style.SUCCESS('Data imported successfully'))

人物.txt:

Johnny Cash,    555555555,  1932-02-26
Sid Vicious,    555555555,  1957-05-10
Axl Rose,   555555555,  1962-02-06
Joey Ramone,    555555555,  1951-05-19
Bruce Dickinson,    555555555,  1958-08-07
Kurt Cobain,    555555555,  1967-02-20
Elvis Presley,  555555555,  2008-19-17

用户.txt:

Johnny Cash
Sid Vicious
Axl Rose
Joey Ramone
Bruce Dickinson
Kurt Cobain
Elvis Presley

【问题讨论】:

    标签: python django python-3.x django-models django-database


    【解决方案1】:

    您好,我还没有足够的声誉对您的帖子发表评论,但如果您只想添加数据以填充 django 中的表格,我建议您使用文档中描述的方法。这种方式更容易,更轻松。

    https://docs.djangoproject.com/en/3.0/howto/initial-data/

    总结:

    • 在您的应用中创建一个“fixtures”目录。
    • 创建要添加的数据文件,该文件与模型字段匹配为 json 或 yml。
    • 致电manage.py loaddata datafile_name

    我希望这会有所帮助。如果没有,你可以评论告诉我,我可以删除它。

    【讨论】:

    • 我试过了,但它不能处理 txt 文档 ''fixtures can be written as JSON, XML or YAML''''
    • 感谢您的提示,如果我没有生病,请设置一个 json 文件,但我需要一个 txt 文件
    猜你喜欢
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 2014-09-23
    • 2013-06-21
    • 2021-09-21
    • 1970-01-01
    相关资源
    最近更新 更多