0

I have created a Django project in which type-1 users can create a Post and type-1 users can Bid on a post. Finally, auction happens and a post_owner can accept a bid. After this whole process, I wanted to remove the post instance from the database, so that it won't appear in post_queryset. For this, I am tried using a signal which is received after an auction and I kept it before the deleting an instance from Post model. But this isn't working. Is there any approach to solve this problem? I would appreciate helping me in achieve this.

Here's my code:

Models.py:

class Post(models.Model):

    item = models.CharField(max_length=20)
    post_owner = models.OneToOneField(settings.AUTH_USER_MODEL, default=1)
    date = models.DateField()
    accepted = models.BooleanField(default = False)



    objects = PostQuerySet.as_manager()


class Bid(models.Model):

    post = models.ForeignKey(Post, related_name = bids)
    amount = models.IntegerField(max_length = 20)
    bidder = models.ForeingKey(settings.AUTH_USER_MODEL)

class Auction(models.Model):

    post = models.OneToOneField(Post)
    post_owner = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)
    bid = models.OneToOneField('truck.Bid', related_name='auctions')

class PostQuerySet(models.QuerySet):

    @receiver(post_save, sender=Auction)
    def delete(self, sender, instance, *args, **kwargs):

        instance.from1.delete
        super(PostQuerySet, self).delete(*args, **kwargs)

forms.py:

class AcceptedForm(forms.ModelForm):


    accepted = forms.BooleanField(required=False)

    class Meta:
        model = Post
        fields = ('accepted', )

Views.py:

def accept_bid(request, post_id, bid_id):

    post = get_object_or_404(Post, id=post_id)
    bid = get_object_or_404(Bid, id=bid_id)
    if request.method=='POST':
        form = AuctionForm(request.POST or None)
        form1 = AcceptedForm(request.POST)
        if form.is_valid() and form1.is_valid():
            accept_bid = form.save(commit=False)
            accept_bid.bid = bid
            accept_bid.post = post
            accept_bid.post_owner = request.user
            accept_bid.accepted = form1.save()
            accept_bid.save()

            form.save()
            form1.save()
    else:
        form = AuctionForm()
        form1 = AcceptedForm()

    context = {

            "bid_id" : bid_id,
            "post_id" : post_id,
            "bid": bid,
            "post":post,
            'form':form,
            'form1': form1,

    }
return render(request, 'loggedin_load/active_deals.html', context)
sumanth
  • 751
  • 2
  • 15
  • 34

1 Answers1

1

If you really want to delete the object, the best place to do that would probably be in the view that you are using to accept the bid, some time after you have used the object, but before you have returned the page to the user. This can be done in Class Based Views by overwriting the appropriate function.

Alternatively, if you are just looking to exclude it from the queryset, you can add a line to your models such as:

accepted = models.BooleanField(default=False)

Simply have your view set this value to True. This way the data will be preserved if a user wants to reverse something. You can then use a queryset like Post.objects.filter(accepted=False) to only retrieve only the Post objects that you are looking for.

As for your signal, maybe someone else can help you get it to work the way you have it set up, but I'm not sure how the signal is supposed to be invoked from within the class. Generally, they aren't placed in classes within the file, but are simply standalone methods. However, I've seen that some prefer to put them inside the model class and label them as an @classmethod.

I prefer to place them as methods in a seperate signals.py file, and route my imports through there:

from My_app.signals import Post
Adam Starrh
  • 6,428
  • 8
  • 50
  • 89
  • @ Adam_Starrh, I did as you said by adding a BooleanField(accepted) in my Post Model. In my accept_bid view i pass this value with a ModelForm (AcceptedForm) as in my question. Here, I made the view for saving an accept_bid instance together with saving the BooleanField Value. But when saving an instance, it returns IntegrityError : (1048, "Column 'date' cannot be null"). Could you please help me on this issue!!! – sumanth Aug 08 '16 at 20:05
  • 1
    Sounds like you either need to deliver information to the `date` field in the model, or set `null=True` on this field. Post your model and views in a new question if you need further assistance. – Adam Starrh Aug 08 '16 at 21:04