Generating model one-to-one field with django-autofixtures
I'm trying to create fake data for my model which is linked to the auth.User. I'm running Python 3.7 with Django 2.1 django-autofixture 0.12.1:
models.py
Class Person(models.Model):
user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True)
about = models.CharField(max_length=200)
autofixtures.py
Class PersonGenerator(AutoFixture):
field_values = {
'user':InstanceGenerator(autofixture=UserFixture(User)),
'about': LoremGenerator(max_length=200)
}
register(Person,PersonGenerator)
Then I run the autofixtures file to register my generator
py manage.py shell
>>> exec(open('myapp/autofixtures.py').read())
>>> exit()
py manage.py loadtestdata myapp.Person:50
I've got the following error message:
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "D:..envlibsite-packagesdjangocoremanagement__init__.py", line 381, in execute_from_command_line
utility.execute()
File "D:..envlibsite-packagesdjangocoremanagement__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:..envlibsite-packagesdjangocoremanagementbase.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "D:..envlibsite-packagesdjangocoremanagementbase.py", line 353, in execute
output = self.handle(*args, **options)
File "c:..localprogramspythonpython37-32Libcontextlib.py", line 74, in inner
return func(*args, **kwds)
File "D:..envlibsite-packagesautofixturemanagementcommandsloadtestdata.py", line 225, in handle
autofixture.create(model, count, **kwargs)
File "D:..envlibsite-packagesautofixture__init__.py", line 136, in create
return autofixture.create(count, **create_kwargs)
File "D:..envlibsite-packagesautofixturebase.py", line 554, in create
instance = self.create_one(commit=commit, **kwargs)
File "D:..envlibsite-packagesautofixturebase.py", line 501, in create_one
self.process_field(instance, field)
File "D:..envlibsite-packagesautofixturebase.py", line 400, in process_field
value = self.get_value(field)
File "D:..envlibsite-packagesautofixturebase.py", line 396, in get_value
value = generator()
File "D:..envlibsite-packagesautofixturegenerators.py", line 71, in __call__
return self.get_value()
File "D:..envlibsite-packagesautofixturegenerators.py", line 67, in get_value
value = self.generate()
File "D:..envlibsite-packagesautofixturegenerators.py", line 535, in generate
return self.autofixture.create()[0]
File "D:..envlibsite-packagesautofixturebase.py", line 554, in create
instance = self.create_one(commit=commit, **kwargs)
File "D:..envlibsite-packagesautofixturebase.py", line 526, in create_one
self.process_m2m(instance, field)
File "D:..envlibsite-packagesautofixturebase.py", line 413, in process_m2m
return self.process_field(instance, field)
File "D:..envlibsite-packagesautofixturebase.py", line 403, in process_field
setattr(instance, field.name, value)
File "D:..envlibsite-packagesdjangodbmodelsfieldsrelated_descriptors.py", line 537, in __set__
% self._get_set_deprecation_msg_params(),
TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use groups.set() instead.
How can it be M2M set when my relationship is one-to-one?
python django
add a comment |
I'm trying to create fake data for my model which is linked to the auth.User. I'm running Python 3.7 with Django 2.1 django-autofixture 0.12.1:
models.py
Class Person(models.Model):
user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True)
about = models.CharField(max_length=200)
autofixtures.py
Class PersonGenerator(AutoFixture):
field_values = {
'user':InstanceGenerator(autofixture=UserFixture(User)),
'about': LoremGenerator(max_length=200)
}
register(Person,PersonGenerator)
Then I run the autofixtures file to register my generator
py manage.py shell
>>> exec(open('myapp/autofixtures.py').read())
>>> exit()
py manage.py loadtestdata myapp.Person:50
I've got the following error message:
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "D:..envlibsite-packagesdjangocoremanagement__init__.py", line 381, in execute_from_command_line
utility.execute()
File "D:..envlibsite-packagesdjangocoremanagement__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:..envlibsite-packagesdjangocoremanagementbase.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "D:..envlibsite-packagesdjangocoremanagementbase.py", line 353, in execute
output = self.handle(*args, **options)
File "c:..localprogramspythonpython37-32Libcontextlib.py", line 74, in inner
return func(*args, **kwds)
File "D:..envlibsite-packagesautofixturemanagementcommandsloadtestdata.py", line 225, in handle
autofixture.create(model, count, **kwargs)
File "D:..envlibsite-packagesautofixture__init__.py", line 136, in create
return autofixture.create(count, **create_kwargs)
File "D:..envlibsite-packagesautofixturebase.py", line 554, in create
instance = self.create_one(commit=commit, **kwargs)
File "D:..envlibsite-packagesautofixturebase.py", line 501, in create_one
self.process_field(instance, field)
File "D:..envlibsite-packagesautofixturebase.py", line 400, in process_field
value = self.get_value(field)
File "D:..envlibsite-packagesautofixturebase.py", line 396, in get_value
value = generator()
File "D:..envlibsite-packagesautofixturegenerators.py", line 71, in __call__
return self.get_value()
File "D:..envlibsite-packagesautofixturegenerators.py", line 67, in get_value
value = self.generate()
File "D:..envlibsite-packagesautofixturegenerators.py", line 535, in generate
return self.autofixture.create()[0]
File "D:..envlibsite-packagesautofixturebase.py", line 554, in create
instance = self.create_one(commit=commit, **kwargs)
File "D:..envlibsite-packagesautofixturebase.py", line 526, in create_one
self.process_m2m(instance, field)
File "D:..envlibsite-packagesautofixturebase.py", line 413, in process_m2m
return self.process_field(instance, field)
File "D:..envlibsite-packagesautofixturebase.py", line 403, in process_field
setattr(instance, field.name, value)
File "D:..envlibsite-packagesdjangodbmodelsfieldsrelated_descriptors.py", line 537, in __set__
% self._get_set_deprecation_msg_params(),
TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use groups.set() instead.
How can it be M2M set when my relationship is one-to-one?
python django
add a comment |
I'm trying to create fake data for my model which is linked to the auth.User. I'm running Python 3.7 with Django 2.1 django-autofixture 0.12.1:
models.py
Class Person(models.Model):
user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True)
about = models.CharField(max_length=200)
autofixtures.py
Class PersonGenerator(AutoFixture):
field_values = {
'user':InstanceGenerator(autofixture=UserFixture(User)),
'about': LoremGenerator(max_length=200)
}
register(Person,PersonGenerator)
Then I run the autofixtures file to register my generator
py manage.py shell
>>> exec(open('myapp/autofixtures.py').read())
>>> exit()
py manage.py loadtestdata myapp.Person:50
I've got the following error message:
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "D:..envlibsite-packagesdjangocoremanagement__init__.py", line 381, in execute_from_command_line
utility.execute()
File "D:..envlibsite-packagesdjangocoremanagement__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:..envlibsite-packagesdjangocoremanagementbase.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "D:..envlibsite-packagesdjangocoremanagementbase.py", line 353, in execute
output = self.handle(*args, **options)
File "c:..localprogramspythonpython37-32Libcontextlib.py", line 74, in inner
return func(*args, **kwds)
File "D:..envlibsite-packagesautofixturemanagementcommandsloadtestdata.py", line 225, in handle
autofixture.create(model, count, **kwargs)
File "D:..envlibsite-packagesautofixture__init__.py", line 136, in create
return autofixture.create(count, **create_kwargs)
File "D:..envlibsite-packagesautofixturebase.py", line 554, in create
instance = self.create_one(commit=commit, **kwargs)
File "D:..envlibsite-packagesautofixturebase.py", line 501, in create_one
self.process_field(instance, field)
File "D:..envlibsite-packagesautofixturebase.py", line 400, in process_field
value = self.get_value(field)
File "D:..envlibsite-packagesautofixturebase.py", line 396, in get_value
value = generator()
File "D:..envlibsite-packagesautofixturegenerators.py", line 71, in __call__
return self.get_value()
File "D:..envlibsite-packagesautofixturegenerators.py", line 67, in get_value
value = self.generate()
File "D:..envlibsite-packagesautofixturegenerators.py", line 535, in generate
return self.autofixture.create()[0]
File "D:..envlibsite-packagesautofixturebase.py", line 554, in create
instance = self.create_one(commit=commit, **kwargs)
File "D:..envlibsite-packagesautofixturebase.py", line 526, in create_one
self.process_m2m(instance, field)
File "D:..envlibsite-packagesautofixturebase.py", line 413, in process_m2m
return self.process_field(instance, field)
File "D:..envlibsite-packagesautofixturebase.py", line 403, in process_field
setattr(instance, field.name, value)
File "D:..envlibsite-packagesdjangodbmodelsfieldsrelated_descriptors.py", line 537, in __set__
% self._get_set_deprecation_msg_params(),
TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use groups.set() instead.
How can it be M2M set when my relationship is one-to-one?
python django
I'm trying to create fake data for my model which is linked to the auth.User. I'm running Python 3.7 with Django 2.1 django-autofixture 0.12.1:
models.py
Class Person(models.Model):
user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True)
about = models.CharField(max_length=200)
autofixtures.py
Class PersonGenerator(AutoFixture):
field_values = {
'user':InstanceGenerator(autofixture=UserFixture(User)),
'about': LoremGenerator(max_length=200)
}
register(Person,PersonGenerator)
Then I run the autofixtures file to register my generator
py manage.py shell
>>> exec(open('myapp/autofixtures.py').read())
>>> exit()
py manage.py loadtestdata myapp.Person:50
I've got the following error message:
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "D:..envlibsite-packagesdjangocoremanagement__init__.py", line 381, in execute_from_command_line
utility.execute()
File "D:..envlibsite-packagesdjangocoremanagement__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:..envlibsite-packagesdjangocoremanagementbase.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "D:..envlibsite-packagesdjangocoremanagementbase.py", line 353, in execute
output = self.handle(*args, **options)
File "c:..localprogramspythonpython37-32Libcontextlib.py", line 74, in inner
return func(*args, **kwds)
File "D:..envlibsite-packagesautofixturemanagementcommandsloadtestdata.py", line 225, in handle
autofixture.create(model, count, **kwargs)
File "D:..envlibsite-packagesautofixture__init__.py", line 136, in create
return autofixture.create(count, **create_kwargs)
File "D:..envlibsite-packagesautofixturebase.py", line 554, in create
instance = self.create_one(commit=commit, **kwargs)
File "D:..envlibsite-packagesautofixturebase.py", line 501, in create_one
self.process_field(instance, field)
File "D:..envlibsite-packagesautofixturebase.py", line 400, in process_field
value = self.get_value(field)
File "D:..envlibsite-packagesautofixturebase.py", line 396, in get_value
value = generator()
File "D:..envlibsite-packagesautofixturegenerators.py", line 71, in __call__
return self.get_value()
File "D:..envlibsite-packagesautofixturegenerators.py", line 67, in get_value
value = self.generate()
File "D:..envlibsite-packagesautofixturegenerators.py", line 535, in generate
return self.autofixture.create()[0]
File "D:..envlibsite-packagesautofixturebase.py", line 554, in create
instance = self.create_one(commit=commit, **kwargs)
File "D:..envlibsite-packagesautofixturebase.py", line 526, in create_one
self.process_m2m(instance, field)
File "D:..envlibsite-packagesautofixturebase.py", line 413, in process_m2m
return self.process_field(instance, field)
File "D:..envlibsite-packagesautofixturebase.py", line 403, in process_field
setattr(instance, field.name, value)
File "D:..envlibsite-packagesdjangodbmodelsfieldsrelated_descriptors.py", line 537, in __set__
% self._get_set_deprecation_msg_params(),
TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use groups.set() instead.
How can it be M2M set when my relationship is one-to-one?
python django
python django
edited Nov 13 '18 at 14:46
Ethan Nguyen
asked Nov 13 '18 at 14:12
Ethan NguyenEthan Nguyen
465
465
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
So Django 2 does not allow creating foreign key without saving the instance first. I switched to factory boy and use their SubFactory feature for this and it works.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282943%2fgenerating-model-one-to-one-field-with-django-autofixtures%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
So Django 2 does not allow creating foreign key without saving the instance first. I switched to factory boy and use their SubFactory feature for this and it works.
add a comment |
So Django 2 does not allow creating foreign key without saving the instance first. I switched to factory boy and use their SubFactory feature for this and it works.
add a comment |
So Django 2 does not allow creating foreign key without saving the instance first. I switched to factory boy and use their SubFactory feature for this and it works.
So Django 2 does not allow creating foreign key without saving the instance first. I switched to factory boy and use their SubFactory feature for this and it works.
edited Nov 16 '18 at 15:31
answered Nov 16 '18 at 15:17
Ethan NguyenEthan Nguyen
465
465
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282943%2fgenerating-model-one-to-one-field-with-django-autofixtures%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown