0

I am building an auction website, and I need to create an Object that shows us the winner of the auction once the auction has been ended! my models.py

class Auction(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    start_time = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    end_time = models.DateTimeField() #when times up Winner should be created Automatically
    start_price = models.DecimalField(verbose_name="Original Price", max_digits=15, decimal_places=1, default=0.0)
    current_price = models.DecimalField(verbose_name="Current Price", max_digits=15, decimal_places=1, default=0.0)
    next_price = models.DecimalField(verbose_name="Next Price", max_digits=15, decimal_places=1, default=0.0)
    step = models.DecimalField(verbose_name="Raise Price", max_digits=15, decimal_places=1, default=0.0)
    sold_price = models.DecimalField(verbose_name="Sold Price", max_digits=15, decimal_places=1, blank=True, null=True)
    is_active = models.BooleanField(default=True)
    is_manual = models.BooleanField(default=True)

    def __str__(self):
        return self.product.title


class Bid(models.Model):
    Types = (
        ('auto', 'Automatically'),
        ('user', 'By user own')
    )
    auction = models.ForeignKey(Auction, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    time = models.DateTimeField(auto_now_add=True)
    price = models.DecimalField(verbose_name="Bid Price", max_digits=15, decimal_places=1, default=0.0)
    action = models.CharField(max_length=120, default="user", choices=Types)

    def __str__(self):
        return str(self.price)


class Winner(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    auction = models.ForeignKey(Auction, on_delete=models.CASCADE)
    won_price = models.DecimalField(verbose_name="Won Price", max_digits=15, decimal_places=1)
    won_time = models.DateTimeField()

    def __str__(self):
        return self.user.username

For the auction process, I am using Django Channels which means I can create the winner if the auction page has viewers(when WebSocket is on) else I am not able to do that! That's why I have to do it on the back-end

and here's my docker-compose.yml file(edited):

version: '3'

services:
  dbauction:
    container_name: mysite
    image: postgres:12.2
    environment:
      POSTGRES_DB: db_mysite
      POSTGRES_USER: user
      POSTGRES_PASSWORD: mysitepass
    volumes:
      - postgres_data:/var/lib/postgresql/mysite/
  mysiteweb:
    container_name: mysiteweb
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - dbauction
    links:
      - redis:redis
    env_file: 
      - ./.env.dev

  redis:
    restart: always
    image: redis:latest
    ports:
      - "6379:6379"
    volumes:
      - redisdata:/data

volumes:
    postgres_data:
    redisdata:
  • Does this help? [Set up a scheduled job?](https://stackoverflow.com/questions/573618/set-up-a-scheduled-job) – Abdul Aziz Barkat Feb 04 '21 at 06:31
  • 1
    To do this, you need a process that runs independent of the django channels process -- for example, a cron job. Do you have something like that already? Maybe celery? Otherwise, there's no _trigger_ that's automatically updating the database when an `Auction.end_time >= datetime.now()`. – damon Feb 04 '21 at 06:32
  • 1
    @damon thanks for your comment, You are right I actually tried to use `apscheduler` but my projects runs in `docker-compose.yml`, I was not lucky with using it! – Jakhongir Alikhanov Feb 04 '21 at 06:38
  • 1
    @JakhongirAlikhanov You can run `apscheduler` using `docker-compose` -- just add it as a new compose service. Install [django-apscheduler](https://github.com/jcass77/django-apscheduler) and add the management command from the readme instructions. Then have your docker container start with the cmd: `python manage.py runapscheduler`. – damon Feb 04 '21 at 20:49
  • @damon I have added my `docker-compose.yml` file too! I have done the instructions! How am I able to add it to my `docker-compose`? can you describe it? thanks for your reply! – Jakhongir Alikhanov Feb 05 '21 at 05:38
  • @JakhongirAlikhanov If it's added as a service in your docker-compose.yml file, you can just run the service using: `docker-compose up`. You should open a new question for this. Make sure you include your current docker-compose.yml and explain what you're attempting to do in detail. Then just link the question here. – damon Feb 05 '21 at 16:47

0 Answers0