You did not quote your models, but I assume your UserProfileDetails has a 
OneToOne or ForeignKey relationship with User model.

With Django, you generally need to save the related model before adding it 
to the Foreignkey, as the relationship in UserProfileDetails is using the 
primary key (called id by default) from User instance, which is populated 
when the object is saved.

I don't quite understand why you don't want to save the User model first, 
the unit tests are running on a separate DB schema.

On Thursday, 16 July 2015 12:21:30 UTC+1, Roland Swingler wrote:
>
> Hi all,
>
> I've very recently come to to Django from a rails background, and 
> struggling to do unit testing of Django models without saving to the 
> database as I'm used to in Rails. Lets say you have 2 linked models, for 
> example User and UserProfileDetails, then in my tests I want to set up the 
> structure and not save any of it:
>
>     user = User()
>     user.profile_details = UserProfileDetails()
>     ...
>     # stuff that tests non-persistence related functionality
>
> But I don't seem to be able to do this, because it will raise a 
> RelatedObjectDoesNotExist error, saying UserProfileDetails has no user. 
> Obviously I can get around this by saving the user first, but if I want to 
> not touch the database in tests is there any way I can achieve this?
>
> (also apologies if this has been asked before - I did a quick search of 
> the archives and couldn't find anything).
>
> Thanks in advance,
> Roland
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2e820843-6bd6-43aa-98fa-bdbf23574d3f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to