I'm new in Django, and I have this problem:
I use self.client to test routes in my app.
fixtures = [
book = Books.objects.get(pk=1)
print(book.name)#it prints "El Quijote" in the test output
response = self.client.get('/sell/book-sell')
self.assertContains(response, "Book Sell")
self.assertContains(response, "El Quijote")#book name
python manage.py test app_name
python manage.py loaddata fixture_name.yaml ...
Finally I discovered what I was doing wrong, thanks to this answer: Django: reload form data after db values change
I had a form like this:
class BookSellForm(forms.Form): books = [('', 'Choose a book')] books.extend(Books.objects.values_list('id', 'name')) libro = forms.ChoiceField(books)
And that was the problem, I had to change to this
class BookSellForm(forms.Form): def __init__(self, *args, **kwargs): super(BookSellForm, self).__init__(*args, **kwargs) books = [('', 'Choose a book')] books.extend(Books.objects.values_list('id', 'name')) self.fields['book'] = forms.ChoiceField(books)
and then everything worked fine, I hope this will be helpful to someone in the future.