I am developing a vocabulary training program with Django (German-Swedish).
The dictionary of the application dictionary consists of a large number of “lexical cards”, each of which contains one or more German words or terms that correspond to one or more Swedish terms.
Training is available only to registered users, as the application monitors the user's performance, saving scorefor each dictionary card.
Dictionary cards have a level (basic, advanced, expert) and any number of tags assigned to them.
When a registered user begins training, the application must calculate the average points of the user for each level and tags so that he can make his choice.
I solved this problem by introducing a model with a name CardByUserthat relates scoreboth to field and ForeignKeyto Userand models Card. Now I can use the Django aggregation function, calculating the average points.
Big flaw: this only works if there is an instance CardByUserfor each card instance that currently exists in the database, even if the user only trained 100 cards. My current solution is to create all of these instances CardByUserduring creation Cardand during user registration. This, of course, is rather vague both in terms of database memory and computational time (user registration takes quite a lot of time).
And that seems totally inelegant, which bothers me the most.
Is there a better way to do this?
Perhaps when calculating the average score for CardDjango, you can specify the following:
- If a
CardByUserfor a given Cardand user exists, use its rating. CardByUser , → 0.
? , ?
:
S.Lott , , . , , - .
class Card(models.Model):
entry_sv = models.CharField(max_length=200)
entry_de = models.CharField(max_length=200)
... more fields ...
class CardByUser(models.Model):
user = models.ForeignKey(User)
card = models.ForeignKey(Card, related_name="user_cards")
score = models.IntegerField(default=0)
... more fields ...
, CardByUser Card.
CardByUser, :
. ():
user_cards = CardByUser.objects.filter(user=current_user)
.filter(card__tags__contains=tag.name)
avg = user_cards_agg.aggregate(Avg('score'))['score__avg']
CardByUser Card , . CardByUser 0.
, ? !