0
votes

It's my first question here, but i will try my best do give you all details. I need to create a watch list for some page, the user should be able to "add" item to watchlist.

ERROR

Exception Type: TypeError
Exception Value: prepare_database_save() missing 1 required positional argument: 'field'

I have no idea why, can you tell me what does 'field' means, I don t even have that name of field in my code. And I am unable to save object to my db.

And Also

C:\auctions\views.py, line 46, in watchlist_add
watchlist.save()

Models

class WatchList(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="user")
    auctions = models.ManyToManyField(Auction, related_name="auctions", blank=True)
    
    def __str__(self):
        return f"{self.user}'s watchlist'"

My URLs

urlpatterns = [
    path("", views.index, name="index"),
    path("watchList/<int:auction_id>", views.watchlist_add, name="watchlist_add"),
]

and Views.py

def getLastPk(obj):
    if(obj.objects.first() is None):
        return 1
    else:
        get_pk = obj.objects.order_by('-pk')[0]
        last_pk = get_pk.pk +1
        return last_pk

def watchlist_add(request, auction_id):
    auction_to_save = Auction.objects.get(pk=auction_id)

    watchlist = WatchList(getLastPk(WatchList),User)
    watchlist.save()
    watchlist.auctions.add(auction_to_save)
    watchlist.save()

    return render(request, "auctions/watchlist.html",{
        "watch_list" : watchlist.auctions.all(),
    })

In html i call this function by:

{% if user.is_authenticated %}
     <a href="{% url 'watchlist_add' listing.id %}">Add to Watch List</a>
{% endif %}

where listing.id is id of current auction

Thank you for any help :)

Full traceback

Traceback (most recent call last):
File "C:\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner response = get_response(request)
File "C:\env\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\env\Scripts\commerce\auctions\views.py", line 63, in wtadd watchlist.save()
File "C:\env\lib\site-packages\django\db\models\base.py", line 753, in save self.save_base(using=using, force_insert=force_insert,
File "C:\env\lib\site-packages\django\db\models\base.py", line 790, in save_base updated = self._save_table(
File "C:\env\lib\site-packages\django\db\models\base.py", line 872, in _save_table updated = self._do_update(base_qs, using, pk_val, values, update_fields,
File "C:\env\lib\site-packages\django\db\models\base.py", line 926, in _do_update return filtered._update(values) > 0
File "C:\env\lib\site-packages\django\db\models\query.py", line 803, in _update return query.get_compiler(self.db).execute_sql(CURSOR)
File "C:\env\lib\site-packages\django\db\models\sql\compiler.py", line 1522, in execute_sql cursor = super().execute_sql(result_type)
File "C:\env\lib\site-packages\django\db\models\sql\compiler.py", line 1143, in execute_sql sql, params = self.as_sql()
File "C:\env\lib\site-packages\django\db\models\sql\compiler.py", line 1478, in as_sql val.prepare_database_save(field),
Exception Type: TypeError at /watchList/add Exception Value: prepare_database_save() missing 1 required positional argument: 'field'

1

1 Answers

0
votes

Ok, I have solved this. I should have passed parameters as user = request.user etc

def watchlist_add(request, auction_id):
    if request.user.is_authenticated:

        watchlist = WatchList(id = getLastPk(WatchList),user = request.user)
        auction_to_save = Auction.objects.get(id=auction_id)    
        watchlist.save()
        watchlist.auctions.add(auction_to_save)
        watchlist.save()

    return render(request, "auctions/watchlist.html",{
        "watch_list" : watchlist.auctions.all(),
    })