0

It's my first time creating a Django website with models, and in my first attempt to insert data into my table I'm getting this error.

My models are as follows:

class User(AbstractUser):
    pass
    #https://docs.djangoproject.com/en/3.1/topics/auth/default/

class Listing(models.Model):
    listingID = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="listID")
    created_by = models.ForeignKey(User, on_delete=models.SET_NULL, related_name="myListing", null=True)
    watchers = models.ManyToManyField(User, blank=True, related_name="watchlist")

    title = models.CharField(max_length=30)
    description = models.TextField()
    creation_date = models.DateField(auto_now=True)
    img_url = models.URLField()
    active = models.BooleanField(default=True)

    def __str__(self):
        return f"{self.title}"

class Bid(models.Model):
    listing = models.ForeignKey(Listing, on_delete=models.SET_NULL, related_name="bidsMadeOnMe", null=True, blank=True)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name="myBids", null=True)

    price = models.FloatField()
    creation_date = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return f"Bid={self.price}"

and the view that handles the form submission is this one:

@login_required
def create_listing(request):
    if request.method == "POST":
        user = User.objects.get(username=request.user.username)
        l = Listing(created_by=user, 
                    title=request.POST["title"], 
                    description=request.POST["desc"], 
                    # https://stackoverflow.com/questions/12176585/handling-dates-over-request-get
                    creation_date=models.DateTimeField(auto_now_add=True), 
                    img_url=request.POST["image_url"]
                    )
        l.save()

        b = Bid(l, 
                user, 
                request.POST["initial_bid"], 
                models.DateTimeField(auto_now_add=True)
                )
        b.save()

    return render(request, "auctions/index.html")

I know the problem is the way I'm adding the data but I can't fix it. Can someone give me some light?

g medina
  • 43
  • 1
  • 8
  • `user = User.objects.get(username=request.user.username)` this code keeps coming back and just won't die! You already have a user in request.user, so don't fetch it again from the database. –  Oct 16 '20 at 20:21
  • Yeah, I found it somewhere else. I'm gonna be more patient next time and actually read the documentation.Thanks again! – g medina Oct 16 '20 at 21:52

1 Answers1

0

Your problem (well, several actually) is this:

b = Bid(l, user, request.POST["initial_bid"], models.DateTimeField(auto_now_add=True))

You're constructing a model instance by positional arguments instead of keyword arguments. This can be done, but then the invisible "id" column that has been added to the Bid model, is the first argument.

In Django we never construct models like that, but always use keyword arguments, so we're not depending on field order:

b = Bid(listing=l, user=user, ...))

Once you're solved that, your next problem is the date field.

Don't assign fields to model instances. Fields are class declarations, they don't belong on instances. Fields describe on a class (= a Model), what kind data to expect. On the instance, you assign that data.

In this case, your definition for the field is wrong on the model and on the instance you shouldn't even assign it - it will be automatically filled.

Overall, it feels like you haven't gone through Django's tutorial or did not fully understand the concepts. I suggest you go through it.

  • Wow thank you so much, I thought the problem was happening while creating a Listing instance but it was actually for the Bid. Things are getting clearer now! – g medina Oct 16 '20 at 21:49