python - User object returns None in ManytoMany -
i have many many relationship between user object , movement object. everytime run test see of user object added, auth.user.none
models:
class movement(models.model): zipcode = models.foreignkey('zipcode') title = models.charfield(max_length=100) description = models.charfield(max_length=400, null=false, blank=true) users = models.manytomanyfield(user) slug = models.slugfield(unique=true) def save(self, *args, **kwargs): self.slug = slugify(self.title) super(movement, self).save(*args, **kwargs) def __str__(self): return self.title
form:
class movementform(forms.modelform): title= forms.charfield(max_length=100, help_text="movement title") description = forms.charfield(help_text="movement description", widget=forms.textarea(attrs={'paceholder':'please enter description'})) slug = forms.charfield(widget=forms.hiddeninput(), required=false) class meta: model = movement exclude = ['zipcode', 'users']
view:
@login_required def new_movement(request): template = 'new_movement.html' if request.method == 'post': zipcode_form = zipcodeform(data=request.post) movement_form = movementform(data=request.post) if zipcode_form.is_valid() , movement_form.is_valid(): zipcode, create = zipcode.objects.get_or_create(**zipcode_form.cleaned_data) movement = movement_form.save(commit=false) movement.zipcode = zipcode movement.save() movement.users.add(request.user) movement.save() return redirect('/%s/results' %zipcode.zipcode) else: zipcode_form = zipcodeform() movement_form = movementform() return render(request, template, {'movement_form':movement_form, 'zipcode_form':zipcode_form}) return render(request, template, {} )
everything runs fine, when run test code hoping name of user, auth.user.none.
test.py:
class newmovementviewtest(testcase): def setup(self): self.user = user.objects.create_user( username='megan', email='megan@email.com', password='password', ) self.client.login(username='megan', password='password') def test_new_movement_saves_user(self): response = self.client.post('/new_movement/', data={'title':'a movement', 'zipcode':20016, 'description':'some movement'}) movements = movement.objects.all() self.assertequal(movements.count(), 1) movement= movements[0] self.assertequal(movement.title, 'a movement') self.assertequal(movement.zipcode.zipcode, 20016) self.assertequal(movement.description, 'some movement') users = movement.objects.filter(users__username__startswith="megan") print(users[0].users)
test output:
creating test database alias 'default'... auth.user.none . ---------------------------------------------------------------------- ran 1 test in 0.191s ok
users = movement.objects.filter(users__username__startswith="megan")
just note naming - going return 'movement' queryset - not user information. will return movement objects based on filter (which in case filtering across relationship); please aware movement objects, not user objects.
you can use indexes first movement object - users[0].users
going return object manager (specifically 'manyrelatedmanager'). if want inspect users associated object, can treat other object managers , like:
user = users[0].users.first()
or
all_users = users[0].users.all()
feel free refer django guide - has nice examples on many-to-many relationships. https://docs.djangoproject.com/en/1.9/topics/db/examples/many_to_many/
Comments
Post a Comment