I have a hasManyThrough releationship in my loopback model. I am trying to follow the Physician/Appointment/Patient example given in the documentation.
Documentation
It says at the very end,
Methods added to the model Once you define a “hasManyThrough”
relation, LoopBack adds methods with the relation name to the
declaring model class’s prototype automatically, for example:
physician.patients.create(...)
One such function is:
physician.patients(filter, function(err, patients) { ... });
I am trying to access this physician relation inside a remote method of Physician model, but it says physician is undefined. In my Physician model also, I cannot see a patients method. If this is the case, how can I use these methods inside a remote method?
Related
I am in a situation where I need to use a custom manager for a related field which is different from the base_manager for the related fields. Let's say that I have a model Item which is in one to many relation with another model Purchase. Now, Item has 2 managers:
objects = ItemManager() #which by default excludes the out_of_stock=True items
complete = CompleteItemManager() #which gives all the items.
For all the other models related to Item, ItemManager is the default one. But in Purchase, I would want to use the CompleteItemManager for the related Item.
So let's say there was once a purchase for an item which is now has out_of_stock=True and we just have the id of that purchase say old_purchase_id, now if try to run the below query:
purchase = Purchase.objects.filter(id=old_purchase_id) # gives error
It would give an error like "Item matching query does not exist" as the manager being used for the related items is ItemManager which excludes such out of stock items.
What can be done in that case? Ideally I would want something to override the manager to be used for a given related field per model, so that even if all other models use ItemManager to resolve item fields, the Purchase Model still uses the CompleteItemManager for its relations with Item.
When accessing a reverse relation you can specify a custom manager to use by calling the related_name and passing the custom manager by name
purchase.item_set.all() # Uses ItemManager
purchase.item_set(manager='complete').all() # Uses CompleteItemManager
Here's how I did it.
I made a CustomForwardManyToOneDescriptor which would just pick the manager named complete for the related model. And used that descriptor in a CustomForeignKey as the forward_related_accessor_class.
class CustomForwardManyToOneDescriptor(ForwardManyToOneDescriptor):
def get_queryset(self, **hints):
related_model = self.field.remote_field.model
return related_model.complete.db_manager(hints=hints).all()
class CustomForeignKey(models.ForeignKey):
forward_related_accessor_class = CustomForwardManyToOneDescriptor
Now using the CustomForeignKey instead of ForeignKey to create OneToMany relation with a model would make that model use the complete manager for the foreign key field no matter what the default related manager is.
I have two different models:
A group model
class Group(models.Model):
(...)
users=models.ManyToManyField(users.User, related_name='trainings')
And a very standard user model.
I'm trying to write a function where it returns all of the linked groups for a given User object.
What would solve my problem is something like this:
def get_groups(user):
connected_groups = Group.objects.filter(user in users)
But that throws an error. It the thing that I am trying possible? Or should I instead create a 'linked_groups' variable within the User model?
Check the documentation here: https://docs.djangoproject.com/en/1.11/topics/db/queries/#many-to-many-relationships
Both ends of a many-to-many relationship get automatic API access to the other end. The API works just as a “backward” one-to-many relationship, above.
The only difference is in the attribute naming: The model that defines the ManyToManyField uses the attribute name of that field itself, whereas the “reverse” model uses the lowercased model name of the original model, plus '_set' (just like reverse one-to-many relationships).
For any User called u in your application, u.group_set.all() will be a queryset of all Groups with a many-to-many relationship to that user. Since you have defined a related_name you can use the more readable syntax u.trainings.all().
Is there any way, to create a private field in Django. I want this field be accessible only from this class's methods.
Here is example of what do I want:
class TestModel(models.Model):
__some_field = models.CharField(max_length=255)
SOLUTION FOUND:
Update: finally, I forge my own solution for this case and added it into my django-fields package, which is available on the GitHub: https://github.com/svetlyak40wt/django-fields
Specifically, if you want to have private fields in your models, then you have to inherit them from my ModelWithPrivateFields class.
Example is available at django-fields's unittests.
Why would you want to do this?
If you just want to hide this field in admin interface you can use name of this field without leading underscore (it doesn't make any difference for Django, as far as I know) and register TestModelAdmin class:
class TestModelAdmin(admin.ModelAdmin):
exclude = ('some_field',)
admin.site.register(TestModel, TestModelAdmin)
I have clients and contacts, and each client can have any number of contacts and each contact can have any number of types. For example, client 1 can have two billing contacts, person A and person B, and two business contacts, person B and person C.
I know it would be possible to model this with the following models:
class Client(models.Model):
id #primary key
#other data
class Contact(models.Model):
id #primary key
#other data
class Relationship(models.Model):
client_id=ForeignKey(Client)
contact_id=ForeignKey(Contact)
type=CharField(max_length=255)#or some other field that represents the type
but this seems incorrect to me because the Relationship model does not represent an object but a relation between other objects. Do I need to do it this way, or is there some way of making the models so that every one actually represents the object
but this seems incorrect to me because the Relationship model does not represent an object but a relation between other objects.
A relationship is a first-class thing.
In a simple RDBMS models, the relationship was implied by a shared key (FK in one, PK in another)
However, when you have many-to-many association tables, you create an explicit row which embodies the relationship.
You're just enriching the association object with additional attributes.
This is fine. It's common, in fact.
It's a generalization of this: https://docs.djangoproject.com/en/1.3/topics/db/models/#many-to-many-relationships
Also, read this: https://docs.djangoproject.com/en/1.3/topics/db/models/#extra-fields-on-many-to-many-relationships
So in my project I am trying to extend the User model to a Staff class and the Group model to a PermGroup class. However, when I save a PermGroup in the Staff's groups field (inherited from User) it only saves the PermGroup object as a Group and all of the fields and methods I defined in my PermGroup class are stripped away. So I decided the best course of action would be to override the groups field. From an earlier stackoverflow question I found and Django documentation, this should work.
class Staff(User):
User.groups = models.ManyToManyField('PermGroup', blank=True)
I need to use 'PermGroup' because the class shows up later in the file, and PermGroup has a field that relies on the Staff class, so if i switched the order I would have the same problem, only in the PermGroup class.
Now the problem I am having is that groups is now a ManyToManyField object where all the other "manytomany" fields are ManyRelatedManagers. I want groups to be a ManyRelatedManager but I do not know how.
Is it possible to get groups to be a ManyRelatedManager when I initiatize it using the 'PermGroup' model call?
If my approach is wrong and you can suggest an alternative to saving PermGroups in the Staff class. I would greatly appreciate it.
Why not just have your Staff be a standard model with a ForeignKey (OneToOneField, to be more exact) to his/her corresponding User?
And, to remove the circular dependency problem, you just need to make one dependent on the other. For instance, the PermGroup model could have a field of a ManytoMany of the Staff members in that group. There's no need for Staff to have a PermGroup, because if you wanted to see what groups a member belongs to, you'd just do something like this:
groups_theyre_in = PermGroups.objects.filter(staff_members__id=id_were_looking_for)