0

This will be my first post here so I hope that everything will be correct.

I got some trouble with django, I try to make a little lottery game. For this game I have one app called bitcoinlottery and in that app 2 models. As for now they look like this:

from django.db import models
from django.utils import timezone

class Lottery(models.Model):
   owner = models.ForeignKey('auth.User')
   title = models.CharField(max_length=100)
   slug = models.SlugField(max_length=110)
   max_players = models.IntegerField()
   total_players = models.CharField(default=0)
   online = models.BooleanField(default=True)
   create_date = models.DateTimeField(default=timezone.now)

class Ticket(models.Model):
   owner = #this most be related to the user that buy the ticket
   lottery_id = #this most be related to the id of the lottery
   ticket_id =  #a random number

Now I have two problems that I can't figure out.

The first one is how to create the number of tickets related on the Lottery max_players, the max_players will be a number of the maximum players/tickets available.

The second question is there a option to see all the available tickets in a list on the admin page of the lotteries?, and if yes, what is the way to do this.

Thanks for any help. Have a nice day.

Danny Vriens
  • 35
  • 1
  • 5

1 Answers1

0

First of all I am not sure if your owner field would work. I don't see any imports for auth package

owner = models.ForeignKey('auth.User')

Firstly I would suggest you look at this post for changing that relation.

Both of your questions need a little bit more clarity regarding what you actually what to achieve. It would be helpful if the Ticket model is also completed.

For the given information its hard to say if that is possible looking there is no relation between both the models.

class Ticket(models.Model):
   # This is not required unless you want it for some reason, you can use
   # this owner from the lotter_id
   owner = #this most be related to the user that buy the ticket
   lottery_id = models.ForiegnKey(Lottery)
   ticket_id =  #a random number

def __str__(self):
    return self.lottery_id, function_that_return_max_tickets()

Using the above relation you can write custom functions in the ModelManager, according to your requirement.

Read through the Mangers that Django provides, these can be used to write those functions which would calculate the max number of tickets and return if you like to use them in views.py using ORM.

Community
  • 1
  • 1
kt14
  • 838
  • 15
  • 25
  • I mark this to solved because with your directions I have accomplished what I need, thanks for the link one more time – Danny Vriens Aug 05 '16 at 17:18