Getting all unique/distinct elements from a many to many field django - mysql

Say that I have this structure:
class Distinct_Alert(models.Model):
entities = models.ManyToManyField(to='Entity', through='Entity_To_Alert_Map')
objects = Distinct_Alert_Manager()
has_unattended = models.BooleanField(default=True)
latest_alert_datetime = models.DateTimeField(null=True)
class Entity_To_Alert_Map(models.Model):
entity = models.ForeignKey(Entity, on_delete=models.CASCADE)
distinct_alert = models.ForeignKey(Distinct_Alert, on_delete=models.CASCADE)
entity_alert_relationship_label = models.TextField()
class Entity(models.Model):
label = models.CharField(max_length=700, blank=False)
related_entities = models.ManyToManyField('self')
identical_entities = models.ManyToManyField('self')
objects = Entity_Manager()
disregarding the other fields, what I'm trying to do is get all the unique entities from a selection of distinct alerts. So say that I pull 3 distinct alerts, and each of them have 4 entities in its manytomany entity field, say that across them, a couple are shared, I want to get only the distinct ones.
I'm doing this:
ret_list = map(lambda x: x.get_dictionary(), itertools.chain(*[alert.entities.all() for alert in
Distinct_Alert.objects.filter(
has_unattended=True,
entities__related_entities__label=threat_group_label)]))
return [dict(t) for t in set([tuple(d.items()) for d in ret_list])]
But as I imagine, this isn't optimal at all since I end up pulling a lot of dupes and then deduping at the end. I've tried pulling the distinct value entities but that pulls me a Long that's used as a key to map the entities to the distinct alert table. Any way to improve this?

Can you try this?
entity_ids = EntityToAlertMap.objects.filter(distinct_alert__has_unattended=True,
entity__related_entities__label=thread_group_label)) \
.values_list('entity', flat=True).distinct()
Entity.objects.filter(id__in=entity_ids)
Django doc about values_list.

Related

how can I fetch the specific ids data( multiple ids are stored in list) using Django

I want to fetch data of multiple ids which is provided from the User Interface and these ids are stored in a list. So how can I retrieve the data of these ids using Django ORM?
When I try the following approach It returned nothing
def selectassess(request):
if request.method=='POST':
assess=request.POST.getlist("assess")
quesno=request.POST.getlist("quesno")
subid=request.POST.getlist("subid")
print(quesno,subid)
print(assess)
max_id = Sub_Topics.objects.all().aggregate(max_id=Max("id"))['max_id']
print(max_id)
pk = random.sample(range(1, max_id),3)
subss=Sub_Topics.objects.raw("Select * from Sub_Topics where id=%s",(str(pk),))
context4={
'subss':subss,
}
print(pk)
return render(request,"assessment.html",context)
By applying the below-mentioned approach I can get only one id-data which is specified by typing the index value. But I want to get the data of all ids which are stored in the list so how can I get my required output by using Django ORM
def selectassess(request):
if request.method=='POST':
assess=request.POST.getlist("assess")
quesno=request.POST.getlist("quesno")
subid=request.POST.getlist("subid")
print(quesno,subid)
print("youuuuu")
print(assess)
max_id = Sub_Topics.objects.all().aggregate(max_id=Max("id"))['max_id']
print(max_id)
pk = random.sample(range(1, max_id),3)
sub = Sub_Topics.objects.filter(pk=pk[0]).values('id','subtopic')
context4={
'sub':sub,
}
print(pk)
return render(request,"assessment.html",context4)
You can use filter(pk__in=pk).
Try:
list_of_ids = random.sample(range(1, max_id),3)
sub = Sub_Topics.objects.filter(id__in=list_of_ids).values('id','subtopic')

How to get field value after distincting with foreinkey

I'm beginner in Djanog and trying to display values that distinct result with foreinkey.
Here are my env and example model.
Django 1.8
Mysql5
Python2.7
class a_group(models.Model):
num = models.AutoField(primary_key=True)
title = models.CharField(max_length=50)
def __unicode__(self):
return self.title
class b_group(models.Model):
no = models.AutoField(primary_key=True)
group = models.ForeignKey(a_group)
And then I tried to distinct with group field like this.
g = b_group.objects.values('group').distinct()
But, as mentioned at here https://docs.djangoproject.com/en/dev/ref/models/querysets/#values , it only return pk, not title.
Is there anyway to get title field value also?
You can also refer to fields on related models with reverse relations through OneToOneField,ForeignKey and ManyToManyField attributes, you can do as follow:
g = b_group.objects.values('group__title').distinct()
to access a field of related model Django use by convention double underscore.

Best association approach to connect Sensor model to others

I'm trying to implement the following with Flask+SQLAlchemy:
I have two database models containing information about bee apiaries and bee hives. I would like to add feature to somehow connect both of them to Sensor model. Those can be attached to one of apiaries or one of bee hives. Here is what I have.
class Apiary(db.Model):
__tablename__ = 'apiary'
# ... fields ...
beehives = db.relationship("BeeHive", backref=db.backref('apiary', lazy='dynamic'), lazy='dynamic')
class BeeHive(db.Model)
__tablename__ = 'beehive'
# ... fields ...
apiary_id = db.Column(db.Integer(), db.ForeignKey('apiary.id'))
class SensorType(db.Model):
__tablename__ = 'sensor_type'
id = db.Column(db.Integer(), primary_key=True)
title = db.Column(db.Unicode(32), unique=True)
sensors = db.relationship('Sensor', backref=db.backref('sensor_type', lazy='dynamic'), lazy='dynamic')
class Sensor(db.Model):
__tablename__ = 'sensor'
id = db.Column(db.Integer(), primary_key=True)
serial = db.Column(UUID(), unique=True)
sensor_type_id = db.Column(db.Integer(), db.ForeignKey('sensor_type.id'))
readings = db.relationship('SensorReading', backref=db.backref('sensor', lazy='dynamic'), lazy='dynamic')
class SensorReading(db.Model):
__tablename__ = 'sensor_reading'
id = db.Column(db.BigInteger(), primary_key=True)
value = # TODO
timestamp = db.Column(db.DateTime(), default=db.func.now())
I was surfing through the internet, reading SQLAlchemy documentation and found something about "polymorphic loading". I have good feeling that this is probably what I was searching for, but don't know how to implement it in my case. I have seen similar thing in "Django world" and they call it "GenericForeignKey".
UPDATE: I have found SQLAlchemy examples about this type of association. Can anyone advice me which of those would be optimum approach? discriminator_on_related, generic_fk, table_per_association or table_per_related? Which of those will be the least headache in further expanding application? Cascading delete?
After two days of experiments I have came to final conclusion. Examples have been taken from this source.
"Discriminator on association" (candidate for answer):
(+) has backward reference
(?) can have 1 parent object
(-) complexity
"Generic Foreign Key":
(+) low complexity
(+) has backward reference
(?) can have 1 parent object
(-) programmer's code must take care of cascading actions
"table per association":
(+) multiple parents
(+) shared table stays intact
(?) number of tables raises with number of associated tables
(-) no backward reference
"table per related" (candidate for answer):
(+) every associated object is in same table
(+) can have multiple tables
(-) associated objects are somehow separated for each foreign table
Answer: "Discriminator on association" as Sensor do not have ability of superposition and therefore no need for multiple parents.

Making the unique validator with Coland and SQLAlchemy

All I trying to do is simple blog website using Pyramid, SQLAlchemy. The form module I have chosen is Deform which uses Coland. So I have for now two fields in my form: name and url. Url creates by transliteration the name field, but it's nevermind. So I don't wanna have two articles with the same urls. I need somehow make the validator with Colland I think. But the problem is the validator performs per field, but not per Model record. I mean if I'd make validator for url field, I dont have information in my method about another fields, such as id or name, so I couldn't perform the validation.
For now I have there couple of strings I created for two hours =)
from slugify import slugify
def convertUrl(val):
return slugify(val) if val else val
class ArticleForm(colander.MappingSchema):
name = colander.SchemaNode(colander.String())
url = colander.SchemaNode(colander.String(),
preparer=convertUrl)
Actually, I thought I should perform such validation on a model level, i.e. in SQLAlchemy model, but of course futher rules don't work, because such rules exist mainly for making SQL scripts (CREATE TABLE):
class Article(TBase, Base):
""" The SQLAlchemy declarative model class for a Article object. """
__tablename__ = 'article'
id = Column(Integer, primary_key=True)
name = Column(Text, unique=True)
url = Column(Text, unique=True)
Actually my question doesn't refer neither to Deform nor to Colander, this validation must be performed at SQLAlchemy level, here's what i've come to:
#validates('url')
def validate_url_unique(self, key, value):
check_unique = DBSession.query(Article)\
.filter(and_(Article.url == value, Article.id != self.id)).first()
if check_unique:
# Doesn't work
raise ValueError('Something went wrong')
# Neither doesn't work
# assert not check_unique
return value

SQL Statement to Single Entity Core Data Fetch Request

I have a single Entity in CoreData mimicking a MySQL database table with the following structure:
Photo
abv Double
photoId Integer16
photographer String
isActive Boolean
lastUpdated Data
name String
I can run the following SQL statement to get my desired result set:
SELECT `photographerName`, COUNT(`photographerName`)
FROM `Photos`
WHERE `isActive` LIKE 1
GROUP BY `photographerName`
ORDER BY `photographerName`
What combination of NSFetchRequest, NSSortDescriptor, NSPredicate, NSExpressionDescription can I use to achieve the same results in iOS?
I am using the following:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Photo"];
NSSortDescriptor *photographerSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"photographer" ascending:YES selector:#selector(localizedCaseInsensitiveCompare:)];
NSPredicate *onlyActivePhotos = [NSPredicate predicateWithFormat:#"isActive == 1"];
request.sortDescriptors = #[photographerSortDescriptor];
request.predicate = onlyActivePhotos;
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
Which gets me the isActive fine, but returns a row for every Photo, not Photographer.
Or... should I be splitting this into two Entities? For example, Photographer with a one-to-many relationship to Photos?
First, it is certainly better and more flexible design to have a relationship Photo-Photographer.
However, what you want can also be achieved. You can only get the photo entities, so after you have to filter the resulting array to just return your photographer names.
NSArray *photographerNames = [fetchedResultsController.fetchedObjects
valueForKeyPath:"#photographer"];
NSSet *uniqueNames = [NSSet setWithArray:photographerNames];
NSArray *orderedNames = [uniqueNames sortedArrayUsingDescriptors:
#[[NSSortDescriptor sortDescriptorWithKey:#"self" ascending:YES]]];
As you can see, this is quite cumbersome. Also, you have lost the information which photos are associated with each photographer.
So please go with the relationship. After all, Core Data is an object graph, not a database wrapper.