So, here is what I want to do.
I have a model Staff, that has a foreign key to the User model. I also have a model Match that has a foreign key to the User model.
I want to select how much Matches every Staff has. I don't know how to do that, so far I only got it working for the User model. From Staff, it will not allow to annotate Match.
This is what is working right now
User.objects.annotate(amount=Count("match")).filter(Q(amount__gt=0)).order_by("amount")
And this is what I wanted to do
Staff.objects.annotate(amount=Count("match")).filter(Q(amount__gt=0)).order_by("amount")
And by the way, is there any way to filter the matches? I want to filter the matches by a certain column.
This wont work?
Staff.objects.annotate(ammount=Count("user__match")).filter(Q(ammount__gt=0)).order_by("ammount")
If both Staff and Match have foreign keys to User, but not to each other, there is no such thing as 'how many matches each staff has'. There are several of both Staff and Match for each User, so there's simply no way of knowing which Staff for a user is related to which Match for that same user.
This isn't a limitation of Django - it's a logical limitation, imposed by the way you've structured your relationships.
Related
By default, in django the group model has the name as unique=True. Is it possible to remove this attribute and how? Does it have any major consequence?
It's probably better to prefix the name of the group with something distinctive rather than try to make it non-unique. By default Group.name is used as a natural key by Django, for serialization purposes.
You could work around display issues by doing something during display, like:
def get_group_name(group):
if "|" in group.name:
return group.name.split("|")[1]
return group.name
group = Group.objects.create(name="COMPANY_X|Sales")
print(get_group_name(group))
# Sales
You can still define your own Group model but it would require customizing your user model quite significantly, which is a lot of work, and there may still be things that rely on Group name uniqueness in Django internals.
I'm using MultiSelectField in my form, which allows the user to select multiple choices:
models.py
physical_limits = MultiSelectField('Physical Limitations', choices=PHYSICAL_LIMIT, max_length=255)
screenshot link
However, I would like to add few additional fields per choice, e.g. min value, max value, units, etc (for the user to fill in, for each choice he makes).
For example, if the user selects option (2), which is "Solid", he will then be able to fill in additional corresponding "sub-fields" for this selection.
In some way, I feel like I'm looking for a different field type, which acts more like a table or array, for the user to fill. I couldn't find such a thing though..
"Vision"
Can you please guide me how to achieve that goal?
Thanks ahead,
Shahar
I am a newbie with Django trying to create a dashboard application reporting on some key milestone dates. I want to be able to track how the key dates are changing. For example:If the kick off date has been changed 5 times I want to be able to report 1. the first date entered, 2. Current date, 3. The date before the last update.
Thank you
Your question is not clear. But for the logic you have asked one thing we can do is to make a model in which the edited dates and user will be fields. Use user as foreign key of your User model. I will just give an example model.
class Dates(models.Model):
event = models.ForeignKey(Event)
date = models.DateField()
This is a very basic method which i am saying. This is a bit complex and you will have to check if the field has changed five times and all.
For a better answer please make the question clear.
I have 3 tables say, TextObj, User, SecurityCheck. The third table has a Foreign Key attribute (textobj) referencing TextObj and there is a many-to-many field (sharedWith) from SecurityCheck to User.
class SecurityCheck(models.Model):
textobj=models.ForeignKey(TextObj)
owner=models.CharField(max_length=255)
sharedWith=models.ManyToManyField(User)
def __init__(self,owner,filename,requestingUsername):
self.owner=owner
self.textobj=TextObj.filter(filename=filename)
self.sharedWith.add(User.objects.filter(username=requestingUsername))
I need to do a query which fetches all the instances of Textobj which have a particular user in the sharedWith field and a particular filename(which is an attribute of TextObj)
You can easily do queries that span (reverse) relationship:
TextObj.objects.filter(securitycheck__sharedWith=user, filename="foo")
https://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships
Django offers a powerful and intuitive way to “follow” relationships in lookups, taking care of the SQL JOINs for you automatically, behind the scenes. To span a relationship, just use the field name of related fields across models, separated by double underscores, until you get to the field you want.
It works backwards, too. To refer to a “reverse” relationship, just use the lowercase name of the model.
I am using the open_graph Explorer tool to test this out and I need to be able to get the first_name, last_name and installed from 1000 users. At the moment I group up the users in grouped of 50 for a single query and batch them into up to 20 queries in a batch request.
A single query looks as follows
?ids=100003825998801,100003825998802,547884299,100003825998803,100003825998804,100003825998805,100003825998806&fields=first_name,last_name,installed
Now, the question is, when you use multiple ids in a single query like I did, facebook seems to "force" certain fields on you, large fields, such as "metadata" and "fields". Is it possible to remove these fields from the result set of the query?
Also, any advice on how to make this more efficient would also be great.
Thank.
Have you considered using FQL instead …?
SELECT first_name, last_name, is_app_user FROM user
WHERE uid IN (100003825998801,100003825998802,547884299,100003825998803,
100003825998804,100003825998805,100003825998806)
– that gives you just the info you want, you only have to watch out for the fact that the installed field is named is_app_user in FQL.