Hello community, I’m new to Django so let me know if there are any references I make that makes no sense and I will provide more specific information.
I’ll start with the problem and afterward how I’ve setup my project so far:
For all the holes I’ve connected to a golfcourse, in django-admin I want to add a score for each hole that the user I choose have scored. But right now I have to add each score hole by hole instead of having a way to list all the 18 holes that are connected to the course. Of course I can make this just a static site, but with the attempt to learn more Django I felt like it was a OK difficulty to start with.
I have added the models (I think is needed): all the users(players), add the years we have played, the golf course we play (for now this has only been one course, but might change in the future), the holes connected to that golfcourse (hole number, hole index, and par for that hole), and also a scorecard where I can select one of the user, what year he played, what course and what he scored for each hole.
This is how my model looks now:
**imports not displayed
class User(models.Model):
"""
User Model. Users will only be added by the Admin.
"""
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
user_bio = models.TextField(max_length=400)
user_hcp = models.DecimalField(
max_digits=5, decimal_places=2,
validators=[MinValueValidator(Decimal('-3.00')),
MaxValueValidator(Decimal('54.00'))])
def __str__(self):
return f"{self.first_name} {self.last_name}"
class Year(models.Model):
"""
Choices of what Year the Competition was played.
"""
year = models.IntegerField()
def __str__(self):
return str(self.year)
class GolfCourse(models.Model):
"""
GolfCourse Model. Information about the course.
"""
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Hole(models.Model):
"""
Hole Model. Connects with the GolfCourse
"""
course = models.ForeignKey(GolfCourse, on_delete=models.CASCADE)
hole_number = models.PositiveIntegerField(validators=[MinValueValidator(1)])
hole_index = models.IntegerField(validators=[MinValueValidator(1)])
hole_par = models.PositiveIntegerField()
class Meta:
unique_together = ('course', 'hole_number')
def __str__(self):
return str(self.hole_number)
class Scorecard(models.Model):
"""
Scorecard to connect a User, what year and course he played, and what he
"""
user = models.ForeignKey(User, on_delete=models.CASCADE)
year_played = models.ForeignKey(Year, on_delete=models.CASCADE)
course = models.ForeignKey(GolfCourse, on_delete=models.CASCADE)
shots_made = models.PositiveIntegerField(default=1,
validators=[
MinValueValidator(1),
MaxValueValidator(18)]
) **# would it be better to have shots made as a ManyToManyField?**
Also in my admin.py I’ve added this to make it easier when I add a course:
class ChoiceInline(admin.StackedInline):
"""Add 18 extra fields when adding a GolfCourse."""
model = Hole
extra = 18
class GolfCouseAdmin(admin.ModelAdmin):
"""Connected to ChoiceInline"""
fieldsets = [
("Golfcourse", {"fields": ["name"]}),
]
inlines = [ChoiceInline]
So based on the problem above, how am I able in django-admin to have it so when I click on “Scorecards” → Add Scorecard, I can choose What user, what Year he played, What Course he played (then best case scenario get a list of all the holes, and another field where I can manually type in that persons score).
Thankful for any help, and I’m excited to learn more Django