I currently have a few models that either do or don't directly relate to a user. User Region and Location.
User is related to Location through a ForeignKey manager and Region is related to Location through a ForeignKey region.
My question is what would be the correct queryset to show the relationship between the User and the Region for example if I were to return all the Regions that belonged to a certain user.
class Region(models.Model):
name = models.CharField(max_length=255)
...
class Location(models.Model):
region = models.ForeignKey(Region, blank=True, null=True)
manager = models.ForeignKey(User, blank=True, null=True)
...
I want to try and avoid having a direct relationship between User and Region if I can help it.
Assuming the user variable is a User instance,
You can have a lookup like this:
regions = Region.objects.filter(location__user=user)
Basically, for the reverse foreign key relationship in the queryset, you would use the lowercase modelname for the field lookup - location in this case.
If such a relationship does not exist, regions.count() would be 0
Documentation of this feature is here
Another way of achieving this is,
regions = user.location_set.values('region') #or values_list - or however you want this
Related
I want my id field to be unique per each customer field. Just like the option unique_for_date from Django (https://docs.djangoproject.com/en/1.11/ref/models/fields/#unique) but in this case, not date but customer.
class Sample(NGObject):
id = models.CharField(max_length=128, null=True, blank=False)
customer = models.ForeignKey(Customer, related_name="blood_samples", on_delete=models.SET(get_default_customer))
I believe this should be done, before the save() method?
When a User writes the wrong ID (that already exists) I would also like to present the information in the Admin Form just like it would for normal unique error.
class Meta:
unique_together = ('sample_id', 'customer',)
This has done the trick :)
I Have an accounts table with account number and account name in a model called accounts
how do I create a lookup such that whenever I enter an account number in django template, account name get populated automatically
my models at attempt are
class Account(models.Model):
account_number = models.IntegerField()
account_name = models.CharField(max_length=50)
bank_name = models.ForeignKey(Bank, on_delete=models.CASCADE)
status = models.CharField(max_length=50)
def __str__(self):
return account_name
def get_absolute_url(self):
return reverse_lazy('accounts')
class AccountLookup(models.Model):
account_number = models.ForeignKey(Account, on_delete=models.CASCADE):
account_name = models. ???????????
If you want something you can use in a template that will render account name starting from an AccountLookup object, then its
{{accountlookup_instance.account_number.account_name}}
Note that this will hit the DB again, unless you used select_related() on the queryset which obtained the AccountLookup object(s) in the first place.
Python code can likewise use this dotted path, with the same caveat.
You might regard it as a simplification to be able to refer to what looks like a field (but isn't). In which case you define it as a property
class AccountLookup(models.Model):
account_number = models.ForeignKey(Account, on_delete=models.CASCADE)
...
#property
def account_name(self):
return self.account_number.account_name
(It gets more useful if you need to apply some standard formatting to the account name to convert it into a more human-readable form in this context).
By the way, calling it account_number is confusing. Better to just name a ForeignKey field for what it is. account in this case: the account object linked by a ForeignKey to this object. Yes, it's represented internally by an account_id which is commonly an integer (the auto-generated primary key), but that's a low level detail Django programmers are not often concerned with.
I've a model as shown below:
class Instance(models.Model):
data_version = models.IntegerField(default=1, unique=True)
name = models.CharField(max_length=200)
The data_version field has to be related to all other models in the application:
class Source(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=200)
version = models.ForeignKey(Instance, on_delete=models.CASCADE, to_field='data_version', null=True)
The problem here is that Django requires the field data_version to be a unique field for me to be able to define such a relationship but that simply doesn't fit into my use case. I need to have multiple Instance objects in the app, each with version numbers starting from 1.
What I'd like is to have a unique constraint on the combination of name and data_version but then Django doesn't allow defining Foreign key relationships as shown above. Is there a way I can bypass this restriction?
I have been struggling with grasping relations for some time and would be very grateful if someone can help me out on this issue.
I have a relation that connects the User model to a ProcessInfo model via one to many and then I have a relation that connects the ProcessInfo to the ProcessAssumptions as One to one
Is there a way to use the User id to get all ProcessAssumptions related to all processes from that user.
I would like to retrieve a queryset of all ProcessAssumptions related to a user id
Here is the model relation :
class ProcessInfo(models.Model):
process_name = models.CharField(max_length=120, null=True)
user_rel = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
class ProcessAssumptions(models.Model):
completion_time = models.FloatField(default='0')
process_rel_process = models.OneToOneField(ProcessInfo, primary_key = True, on_delete=models.CASCADE)
Using field referencing for foreign keys.
process_assumption_objects = ProcessAssumptions.objects.filter(process_rel_process__user_rel=<user_id>)
Replace <user_id> with the id you wish to query for.
When you define a relationship to model X in another model Y, all related Ys can be accessed from an instance of X by X_instance.Y_set.all(). You can even perform the regular filter or get operations on that. X_instance.Y_set is the default object manager for Y (same as Y.objects), but it's filtered to only contain the objects that are related to X_instance.
So in this specific case, you can get all ProcessInfo objects for a certain user like this:
user = User.objects.get(the_user_id)
required_assumptions = [proc_info.process_assumptions for proc_info in user.process_info_set.all()]
This might be a bit hard to read with _set suffix, so you can define a related_name argument while defining the relation on the model.
like:
# in class ProcessInfo
user_rel = models.ForeignKey(User, null=True, on_delete=models.SET_NULL, related_name='processes')
# and now you can do
some_user.processes.all()
Model
class SlackPermission(models.Model):
#fields
class GithubPermission(models.Model):
#fields
class Employee(models.Model):
#fields
slack_permission = models.OneToOneField(SlackPermission, on_delete=models.CASCADE, related_name='Slack',default=SlackPermission.objects.get(pk=1))
github_permission = models.OneToOneField(GithubPermission, on_delete=models.CASCADE, related_name='Github',default=GithubPermission.objects.get(pk=1))
Error:
ValueError: Cannot serialize: <GithubPermission: GithubPermission object (1)>
There are some values Django cannot serialize into migration files.
I am creating API just to create Employee. Where there is not option of giving slackpermissions and githubpermissions. How do I give default value in there?
The problem is that the default is calculated immediately, and for migrations, it can not really serialize that.
That bing said, it is not very useful to do this anyway. You can just pass the primary key as default value. This is specified in the documentation on the default=… parameter [Django-doc]:
For fields like ForeignKey that map to model instances, defaults should be the value of the field they reference (pk unless to_field is set) instead of model instances.
So we can write this as:
class Employee(models.Model):
full_name = models.CharField(max_length=100)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
slack_permission = models.OneToOneField(
SlackPermission,
on_delete=models.CASCADE,
related_name='Slack',
default=1
)
github_permission = models.OneToOneField(
GithubPermission,
on_delete=models.CASCADE,
related_name='Github',
default=1
)
Note that you should ensure that there exists an object with that primary key. Therefore it might not be ideal to do that.
The issue here is that you are attempting to set a field value to an object instance. So your default value should be just 1 if you are certain of the pk.
Also, I am not sure the advantage of creating two separate models for these permission values. Seems like they can just be fields in your employee model. Seems like these permissions share identical fields as well which will allow you to flatten them a bit.