I have a table in which data gets written in a very fast rate (around 40000 writes per minute)
class dummyclass():
field1 = models.CharField()
field2 = models.IntegerField()
...
field6 = models.DecimalField()
There are about 6 to 8 fields in it which is constantly changing
So i decided to split this class into six different classes like this
class dummyclass():
field1 = models.CharField()
field2 = modles.CharField()
class subdummyclass():
dummy = models.ForiegnKeyField(dummyclass)
field3 = models.CharField()
class subdummyclass1():
dummy = models.ForiegnKeyField(dummyclass)
field4 = models.CharField()
class subdummyclass2():
dummy = models.ForiegnKeyField(dummyclass)
field5 = models.CharField()
class subdummyclass3():
dummy = models.ForiegnKeyField(dummyclass)
field6 = models.CharField()
is there any advantage in splitting data to different tables like this or am i overdoing this (overnormalization) .
Any help is appreciated
Thanks and regards
Related
I'm trying to calculate the length of a house relative to some adjacent neighborhoods, such that, house_size / (neighborhood[0].length + neighborhood[1].length...):
class House(models.Model):
house_id = models.CharField(max_length=256, primary_key=True)
size = models.IntegerField()
neighborhood = models.ManyToManyField('neighborhood', through='HouseNeighborhoodLink')
class Neighborhood(models.Model):
length = models.IntegerField()
I created a table to assign several neighborhoods with a house. Also, houses can be assigned to several adjacent neighborhoods:
class HouseNeighborhoodLink(models.Model):
house = models.ForeignKey(House, on_delete=models.DO_NOTHING)
neighborhood = models.ForeignKey(Neighborhood, on_delete=models.DO_NOTHING)
Serializers:
class LengthFromNeighborhoodSerializer(serializers.ModelSerializer):
class Meta:
model = Neighborhood
fields = ['length']
class HouseCoverageOfNeighborhood(serializers.ModelSerializer):
Neighborhood = LengthFromNeighborhoodSerializer(read_only=True)
class Meta:
model = HouseNeighborhoodLink
fields = ['house_id', 'Neighborhood']
I'm stuck in the three dots (...), where I want to access the length attribute of all neighborhoods and then calculate the proportion to a house. I'm not sure how to access the length of each neighborhood from HouseNeighborhoodLink table:
class HouseCoverageDetail(generics.ListAPIView):
queryset = HouseNeighborhoodLink.objects.all()
serializer_class = HouseCoverageOfNeighborhood
def get_queryset(self):
house_id = self.kwargs['house_id']
neighborhood = self.queryset.filter(house_id=house_id)
...
return result
Chack this
class HouseCoverageOfNeighborhood(serializers.ModelSerializer):
Neighborhood = LengthFromNeighborhoodSerializer(many=True,read_only=True)
class Meta:
model = HouseNeighborhoodLink
fields = ['house_id', 'Neighborhood']
hi I am trying to get the same value in a many to many tables but I don't know how can I achieve that
here is my model:
class Raza(models.Model):
Nombre = models.CharField(max_length=50)
Origen = models.CharField(max_length=45)
Altura = models.CharField(max_length=10)
Peso = models.CharField(max_length=10)
Esperanza_vida = models.CharField(max_length=10)
Actividad_fisica = models.CharField(max_length=45)
Recomendaciones = models.CharField(max_length=500)
Clasificacion_FCI = models.ForeignKey(Clasificacion_FCI,null=True,blank=True,on_delete=models.CASCADE)
Tipo_pelo = models.ManyToManyField(Tipo_pelo,blank=True)
Caracteristicas_fisicas = models.ManyToManyField(Caracteristicas_fisicas,blank=True)
Caracter = models.ManyToManyField(Caracter,blank=True)
Ideal = models.ManyToManyField(Ideal,blank=True)
Tamanio = models.ForeignKey(Tamanio,null=True,blank=True,on_delete=models.CASCADE)
User = models.ManyToManyField(User,blank=True)
I am using the User model that's provided by Django
I have no idea how can I do that
I want do something like that
table user
id_usuario = 1
name = "Juan"
table raza
id_raza = 1
name = "pitbull"
table user_raza
id_user_raza = 1
id_user = 1
id_raza = 1
Please write class attributes with small letters in python.
to your question:
raza = Raza.objects.get(id=1)
tipo_pelos = raza.tipo_pelo.all()
for tp in tipo_pelos:
print(tp.id)
...
...
Django has wonderful documentation for this. Have fun.
I am trying to do a reverse lookup in Django with multiple conditions, hopefully, my question does not duplicate with other questions. Let me explain:
I have two models:
Model A:
class A(models.Model):
title = models.CharField(max_length=200)
... other fields
Model B:
class B(models.Model):
a = models.ForeignKey(A)
label = models.CharField(max_length=200)
value = models.CharField(max_length=200)
... other fields
How can I get A that:
1) has a B with label = name, value=John
2) and, has a B with label =age, value =20
I tried the following, it does not work:
A.objects.filter(b__label="name", b__value="John", b__label="age", b__value=20)
You can use Q object:
from django.db.models import Q
A.objects.filter(
(Q(b__label="name") & Q(b__value="John")) | (Q(b__label="age") & Q(b__value=20))
)
I have a models.py like so:
class ScoreCard(models.Model):
user = models.ForeignKey(User)
course = models.ForeignKey(Course)
created = models.DateTimeField(default=datetime.now)
score = models.IntegerField()
holes = models.IntegerField(
default=9
)
handicap = models.IntegerField(default=0)
class UserProfile(User):
...
def get_nine_stats(self, course):
nine_stats = ScoreCard.objects.filter(course=course) \
.filter(holes=9) \
.aggregate(Avg('score'), Max('score'), Min('score'))
return nine_stats
This is fine and returns almost what I need - a dict of max, min and avg. However, I really want those values with the associated created fields. Is this possible using the aggregate method?
Imagine a 5x5 grid (map), every field of it represents a certain object (it can be a monster, a tree etc.)
So, here we have:
class Field(Model):
x = y = PositiveIntegerField()
content = ...(?)
Here the problem arises. Here is the alternative, but I think this way is too messy, especially if I have many different content ids.
class Field(Model):
x = y = PositiveIntegerField()
content = PositiveIntegerField()
monster_rel = ForeignKey(Monster, null=True, blank=True)
building_rel = ForeignKey(Monster, null=True, blank=True)
nature_obj_rel = ForeignKey(Monster, null=True, blank=True)
and then in a view:
f = Field.objects.get(pk=1)
if f.content == 1:
print "Monster %s of level %d" % (f.monster_rel.name, f.monster_rel.level)
elif f.content == 2:
print "This is a %s" % f.building_rel.type
...
Is there a better solution for this?
EDIT
I would like fields like:
content_id = IntegerField()
content_rel = FieldRelatedToModelByContentId()
Well, sounds like generic relations is exactly what you're looking for.