Hi,
    Go through this. This will help you. The error is because of
foreign key.

https://stackoverflow.com/questions/37839867/django-error-cannot-assign-must-be-an-instance

On Thu, Sep 3, 2020 at 8:46 PM Abdu-H <[email protected]> wrote:

> Hello,
>
> I am working on an app where car owner will list their car and drivers
> interested in renting them will make a request to rent the cars…
>
> i basically have three models the *users*(car owners and drivers) *car*
> listed by owners, and *rent, *i have a listView to list all the car
> available and a detailView for each car, i have a button on the detailed
> view, when clicked i want it to take some of the information on the
> detailed view and save it in the* rent *model… But i am getting a
> ValueError (Cannot query "testuser1": Must be "Listing" instance). But i
> also need to store the driver(testuser1 in this case) in the Rent model.
>
> Thanks in advance... any help is hugely appreciated
>
> Below are my view.py, models.py and error messages:
>
>
>
> *view.py*
>
> class ListingDetailView(DetailView):
>
>     model = Listing
>
>
>     def post_rent(self, request, pk, *args, **kwargs):
>
>         user = request.User
>
>         listing = get_object_or_404(Listing, pk=pk)
>
>         rent, created = Rent.objects.get_or_create(
>
>             car_owner=listing.car_owner,
>
>             driver=request.user,
>
>             car_id=listing.pk)
>
>         rent.save(request.POST)
>
>         messages.info(request, *"Rent request has been made succesfully."*
> )
>
>         return redirect(*'/'*)
>
>
>
> *models.py*
>
> class Listing(models.Model):
>
>     DEF_YEAR = *'2003'*
>
>     CAR_YEAR_CHOICES = [(*'2002'*, *'2002'*), (*'2003'*, *'2003'*), (
> *'2004'*, *'2004'*), (*'2005'*, *'2005'*),
>
>                         (*'2006'*, *'2006'*), (*'2007'*, *'2007'*), (
> *'2008'*, *'2008'*), (*'2009'*, *'2009'*),
>
>                         (*'2010'*, *'2010'*), (*'2011'*, *'2011'*), (
> *'2012'*, *'2012'*), (*'2013'*, *'2013'*),
>
>                         (*'2014'*, *'2014'*), (*'2015'*, *'2015'*), (
> *'2016'*, *'2016'*), (*'2017'*, *'2017'*),
>
>                         (*'2018'*, *'2018'*), (*'2019'*, *'2019'*), (
> *'2020'*, *'2020'*), ]
>
>
>     NEW = *'New'*
>
>     USED = *'Used'*
>
>     OLD = *'Old'*
>
>     CAR_CONDITION_CHOICES = [(*'NEW'*, *'New'*), (*'USED'*, *'Used'*), (
> *'OLD'*, *'Old'*)]
>
>
>     car_owner = models.ForeignKey(User, on_delete=models.CASCADE)
>
>     car_make = models.CharField(max_length=50)
>
>     car_model = models.CharField(max_length=50)
>
>     car_year = models.CharField(max_length=4, choices=CAR_YEAR_CHOICES,
> default=DEF_YEAR)
>
>     car_condition = models.CharField(max_length=4,
> choices=CAR_CONDITION_CHOICES, default=OLD)
>
>     car_pic1 = models.ImageField(default=*'car.png'*, upload_to=
> *"car_listing-photos"*)
>
>     weekly_charges = models.FloatField()
>
>
>     def __str__(self):
>
>         return *f'*{self.car_year}  {self.car_make}  {self.car_model}*'*
>
>
>     def get_absolute_url(self):
>
>         return reverse(*'listing-detail'*, kwargs={*'pk'*: self.pk})
>
>
>
> class Rent(models.Model):
>
>     car_id = models.BigIntegerField(null=True, blank=True)
>
>     car_owner = models.ForeignKey(Listing, on_delete=models.SET_NULL,
> null=True, blank=True)
>
>     driver = models.ForeignKey(User, on_delete=models.SET_NULL, null=True,
> blank=True)
>
>     start_date = models.DateField(null=True, blank=True)
>
>     end_date = models.DateField(null=True, blank=True)
>
>     approved = models.BooleanField(default=False)
>
>
> *listing_detail.html*
>
> <form method="POST">
>
>     {% csrf_token %}
>
> <button type="submit" name="hire-car" class="btn btn-primary">Rent
> Car</button>
>
>  </form>
>
>
>
> *Error Message*
>
>
> *ValueError at /listing/2/*
>
> Cannot query "testuser1": Must be "Listing" instance.
>
> *Request Method:*
>
> POST
>
> *Request URL:*
>
> http://127.0.0.1:8000/listing/2/
>
> *Django Version:*
>
> 3.1
>
> *Exception Type:*
>
> ValueError
>
> *Exception Value:*
>
> Cannot query "testuser1": Must be "Listing" instance.
>
>
> *More Error Dump:*
>
>
> Environment:
>
>
>
> Request Method: POST
>
> Request URL: http://127.0.0.1:8000/listing/2/
>
>
> Django Version: 3.1
>
> Python Version: 3.8.5
>
> Installed Applications:
>
> ['accounts.apps.AccountsConfig',
>
>  'django.contrib.admin',
>
>  'django.contrib.auth',
>
>  'django.contrib.contenttypes',
>
>  'django.contrib.sessions',
>
>  'django.contrib.messages',
>
>  'django.contrib.staticfiles',
>
>  'app.apps.AppConfig',
>
>  'crispy_forms']
>
> Installed Middleware:
>
> ['django.middleware.security.SecurityMiddleware',
>
>  'django.contrib.sessions.middleware.SessionMiddleware',
>
>  'django.middleware.common.CommonMiddleware',
>
>  'django.middleware.csrf.CsrfViewMiddleware',
>
>  'django.contrib.auth.middleware.AuthenticationMiddleware',
>
>  'django.contrib.messages.middleware.MessageMiddleware',
>
>  'django.middleware.clickjacking.XFrameOptionsMiddleware']
>
>
>
>
> Traceback (most recent call last):
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/core/handlers/exception.py",
> line 47, in inner
>
>     response = get_response(request)
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/core/handlers/base.py",
> line 179, in _get_response
>
>     response = wrapped_callback(request, *callback_args, **callback_kwargs)
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/views/generic/base.py",
> line 73, in view
>
>     return self.dispatch(request, *args, **kwargs)
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/views/generic/base.py",
> line 101, in dispatch
>
>     return handler(request, *args, **kwargs)
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/app/views.py",
> line 32, in post
>
>     rent, created = Rent.objects.get_or_create(
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/db/models/manager.py",
> line 85, in manager_method
>
>     return getattr(self.get_queryset(), name)(*args, **kwargs)
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/db/models/query.py",
> line 573, in get_or_create
>
>     return self.get(**kwargs), False
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/db/models/query.py",
> line 418, in get
>
>     clone = self._chain() if self.query.combinator else self.filter(*args,
> **kwargs)
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/db/models/query.py",
> line 942, in filter
>
>     return self._filter_or_exclude(False, *args, **kwargs)
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/db/models/query.py",
> line 962, in _filter_or_exclude
>
>     clone._filter_or_exclude_inplace(negate, *args, **kwargs)
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/db/models/query.py",
> line 969, in _filter_or_exclude_inplace
>
>     self._query.add_q(Q(*args, **kwargs))
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/db/models/sql/query.py",
> line 1358, in add_q
>
>     clause, _ = self._add_q(q_object, self.used_aliases)
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/db/models/sql/query.py",
> line 1377, in _add_q
>
>     child_clause, needed_inner = self.build_filter(
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/db/models/sql/query.py",
> line 1292, in build_filter
>
>     self.check_related_objects(join_info.final_field, value,
> join_info.opts)
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/db/models/sql/query.py",
> line 1119, in check_related_objects
>
>     self.check_query_object_type(value, opts, field)
>
>   File
> "/Users/ChuzzyOfficial/PycharmProjects/djangoProject/instarydes/.venv/lib/python3.8/site-packages/django/db/models/sql/query.py",
> line 1100, in check_query_object_type
>
>     raise ValueError(
>
>
> Exception Type: ValueError at /listing/2/
>
> Exception Value: Cannot query "testuser1": Must be "Listing" instance.
>
>
>
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/4aae705a-c42b-4c94-b98b-4ed372e5a33cn%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/4aae705a-c42b-4c94-b98b-4ed372e5a33cn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Thanks and Regards

J. Ranga Bharath
cell: 9110334114

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK5m314kzT7b%3DBD%3DHYb_%2BfeNy%3DDgcMELgJDs2TYKfQu-kyhXFA%40mail.gmail.com.

Reply via email to