How to Compare ManyToManyField to another ManyToManyField - python

I have a model of Partner
class Partner(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
group = models.OneToOneField(
Group, on_delete=models.DO_NOTHING, blank=True, null=True)
def __str__(self):
return self.name
I have 2 other models one is CustomUser and other is Quote
class CustomUser(AbstractUser):
#...
name = models.CharField(max_length=160, null=True, blank=True)
partner = models.ManyToManyField(
Partner, blank=True)
class Quote(models.Model):
#...
visibility = models.CharField(max_length=10)
partner = models.ManyToManyField(
Partner, blank=True)
Both have a partner field related with ManyToManyField to Partner Model
Now I want to compare them in the views like:
partner field can have multiple partners like partner1, partner2, partner3
how to to find the partners matching to each other inside the Quote and CustomUser model
Lets say, One of the Quote object have set of [partner1 and partner6] in the ManyToManyField and I only want to have access to that quote to users who also have partner1 and partner6 in their partner ManyToManyField set.
So how can I filter and compare them ?
I also read the docs but didn't able to reproduce the solution. help would be appreciated.
Edit : I can explain it a little , lets say From whole set of partner's in the quote if even one partner is matched to set of partners to the CustomUser then CustomUser should also have access to it.

You can .filter(…) [Django-doc] with:
Quote.objects.filter(partner__customuser=my_user)
This will return a QuerySet of Quotes that have at least one Partner in common with my_user.
The same Quote will be returned that many times as there are Partners in common. You can use .distinct() [Django-doc] to avoid that:
Quote.objects.filter(partner__customuser=my_user).distinct()

Related

One membership for each user for each club

I'm creating models to manage clients and sports centers. Each customer can be enrolled in several centers only once of course. how should i proceed for this?
class Membership(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='memberships')
club = models.ForeignKey(Club, on_delete=models.CASCADE, related_name='memberships')
points = models.PositiveIntegerField(default=0, blank=True, null=True)
sub_date = models.DateField(auto_now_add=True)
#other...
If I proceed in this way it is possible to create multiple subscriptions of the user to the same club. How can I solve the problem?
Use unique_together in the model Meta so that a Membership object cannot have the same user and club as another object:
class Membership(models.Model):
...
class Meta:
unique_together = ['user', 'club']
This will return an error if you use a form or the Django admin to create a membership that already exists with that user/club.
Use m2m relationship.
field = models.ManyToManyField(Your_model)

In Django Tables2, how do you make a column show text from a table referenced by a foreign key?

After reading all the docs and answers I can find, and burning a whole day, I still can't make this work. Using Django Tables2, I want to show a list of instruments; the instruments table includes a foreign key to an instrumentsType table. When I list the instruments and their attributes, I want to use the foreign key to substitute the textual instrument type description from the other table. I have tried every combination of double underscores and other accessor techniques, but so far all I get is the dreaded -- in the column. (Displaying just the record ID works).
from .models import Instrument
from django_tables2 import A
from instrumenttypes.models import InstrumentType
class InstrumentTable(tables.Table):
id = tables.LinkColumn('instrument_details', args=[A('station_id')])
class Meta:
model = Instrument
template_name = "django_tables2/bootstrap.html"
fields = ("id", "instrument", "nickname", "serialNo",
"instrument__instrumenttype_id__instrumenttypes__id_instrumentType" )
The models involved are:
Instruments model.py
from django.db import models
from instrumenttypes.models import InstrumentType
from stations.models import Station
# Create your models here.
class Instrument(models.Model):
instrument = models.CharField(max_length=40)
instrumenttype = models.ForeignKey(InstrumentType, on_delete=models.CASCADE, null=True)
station = models.ForeignKey(Station, on_delete=models.CASCADE, default=1)
serialNo = models.CharField(max_length=60, null=True, blank=True)
dateAdded = models.DateTimeField("Date Added", null=True, blank=True)
dateRemoved = models.DateTimeField("Date Removed", null=True, blank=True)
status = models.CharField(max_length=10, null=True, blank=True)
nickname = models.CharField(max_length=40, null=True, blank=True)
InstrumentTypes model.py
from django.db import models
class InstrumentType(models.Model):
instrumentType = models.CharField(max_length=40)
Resulting output:
ID Instrument Nickname SerialNo Instrumenttype
4 instr2 nock2 123 —
The most relevant online references I have found are here and here; but having tried the suggestions, no luck. What am I missing?
I've been struggling to get something working too (but I finally did), and I found the examples too brief.
I think you want to get rid of this stuff in the Meta class
"instrument__instrumenttype_id__instrumenttypes__id_instrumentType"
I think Meta.fields should just be a list of field names, and that you refer to the attribute in the other table from the point of view of the type of object you will later pass in to the IntrumentTable constructor (and that is named in the Meta.model attribute:
from django_tables2.utils import Accessor
class InstrumentTable(tables.Table):
instrument_type = tables.Column(accessor=Accessor('instrumenttype.name'))
class Meta:
model = Instrument
template_name = "django_tables2/bootstrap.html"
fields = ("id", "instrument", "nickname", "serialNo", "insrument_type")
Then, in view, make an instance of InstrumentTable
def myview(request):
table_to_render = InstrumentTable(Instrument.objects)
return render(request, sometemplate, {table: table_to_render})
You didn't show your view, and I know there may be a different way. If you have the whole thing in a repo somewhere, leave a link.

Django - Making a Reservation system

As a part of a task, I created an "apartment listings" website.
I managed to get that done, but now I need to make a "reservation system" for it.
The basic idea is that a logged in user can select an apartment from the listed apartments, pick a "start_date" and an "end_date" (if the apartment already isn't booked ) and book the apartment.
Im a total Django newbie, and need some pointers in order to start somewhere with this task.
I have an Apartment model which contains all of the Apartments info that I use to print everything out with the template.
I'm using the provided django user models to register / log-in.
What kind of model do I need for the reservation, and how could I connect everything ?
I tried making a reservation model, but I got no idea where to go next.
I'm not asking for you to solve this for me, I'm asking for someone to explain (if possibile in detal) to me how could I go on about achieving this on my own.
this is my apartment model:
class Apartment(models.Model):
title = models.CharField(max_length=200)
address = models.CharField(max_length=200)
city = models.CharField(max_length=100)
state = models.CharField(max_length=100)
zipcode = models.CharField(max_length=20)
description = models.TextField(blank=True)
price = models.IntegerField()
bedrooms = models.IntegerField()
bathrooms = models.DecimalField(max_digits=2, decimal_places=1)
garage = models.IntegerField(default=0)
size = models.IntegerField()
photo_main = models.ImageField(upload_to='photos/%Y/%m/%d/')
photo_1 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo_2 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo_3 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo_4 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
in_rent = models.BooleanField(default=False)
list_date = models.DateTimeField(default=datetime.now, blank=True)
def __str__(self):
return self.title
Thank you so much !
Your assumption for making a Reservation object would be correct.
You'll want to link those reservations to the Apartment in question. Django supports these kind of relationships through their ForeignKey and ManyToManyField.
These fields support linking of one-to-one, one-to-many, and many-to-many relationships.
In your case, one Apartment can have many Reservations, which means you need to have a field in your Reservation pointing to a single Apartment, which can be done using a ForeignKey
Your model should look something like this:
Reservation(models.Model)
apartment = models.ForeignKey(to=Apartment, related_name='reservations',
on_delete=models.Cascade)
start_date = models.DateField()
end_date = models.DateField()
To retrieve all reservations for an appartment, you can simply use apartment.reservations since we've defined a related_name in the ForeignKey field.
You can even filter for specific dates by doing Reservations.objects.filter(apartment=apartment, start_date__gte=<your_start_date>, end_date__gte<your_end_date>)

Django ORM relations one-to-many

I have a model:
class Product(models.Model):
title = models.CharField(max_length=200)
url = models.URLField()
pub_date = models.DateTimeField()
votes_total = models.IntegerField(default=1)
image = models.ImageField(upload_to='images/')
icon = models.ImageField(upload_to='images/')
body = models.TextField()
hunter = models.ForeignKey(User, on_delete=models.CASCADE)
Now I'd like to add a functionality of upvoters to know on what products user has already voted. I need this to allow users vote on the one product only once.
Again, to clarify - user can vote on several products but only once on each.
So the relation is one product - many users (upvoters).
I tried to add the next field but cannot make a migration even if default field is provided. Also I tried to clear the database but again cannot make it work.
upvoters = models.ForeignKey(User, on_delete=models.CASCADE, related_name='upvoted')
I suppose it works the next way:
Field to determine upvoted products.
To check if user has been upvoted on product, call: User.upvoted.filter(id=product.id).count() == 1
This means that user has already upvoted on this product.
What's wrong? What should I change to make it work?
You will have to use ManyToMany, but you can use a custom through model to restrict the product/vote combinations.
To Product class, add:
voters = models.ManyToManyField(User, through='ProductVote', related_name='product_voters')
Then add the custom through model:
class ProductVote(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
product = models.ForeignKey(Vote, on_delete=models.CASCADE)
class Meta:
unique_together = ['user', 'product']
If you try to add a vote for the same user/product combination, an IntegrityError will be raised.

Django Admin: Add foreign keys at same time as model

I am a novice in Django and I'm learning the ropes of the admin interface. I have a model with several foreign keys. These foreign keys then reference other foreign keys. On the admin website after I register the Property model and then try to add it I am given a dropdown box for each foreign key model. However this dropdown box only lists existing foreign keys. (http://i.stack.imgur.com/e5LCu.png)
What would be great is if instead of a dropdown box there were extra fields so I could add the foreign key models as I add the property model. That way I wouldn't have to manually add foreign keys and then go back and add some more, and then go back and finally add the property data.
How can I do this? This feels like a simple enough question but after intense Googling I still can't find the answer, so I apologize in advance.
Example of two of my models:
class Address(models.Model):
state = models.ForeignKey('State')
address1 = models.CharField(max_length=200)
address2 = models.CharField(max_length=200)
city = models.CharField(max_length=200)
postal_code = models.CharField(max_length=200)
class Property(models.Model):
address = models.ForeignKey('Address', blank=True, null=True)
borrower = models.ForeignKey('Person', blank=True, null=True)
company = models.ForeignKey('Company', blank=True, null=True)
contract = models.ForeignKey('Contract', blank=True, null=True)
loan_balance = models.IntegerField()
name = models.CharField(max_length=200)
primary_email = models.CharField(max_length=200)
primary_phone = models.CharField(max_length=200)
property_no = models.IntegerField()
Example of my admin.py:
# Register your models here.
class PropertyAdmin(admin.StackedInline):
model = Property
class PersonAdmin(admin.StackedInline):
model = Person
class CompanyAdmin(admin.StackedInline):
model = Company
class ContractAdmin(admin.StackedInline):
model = Contract
class CompletePropertyAdmin(admin.ModelAdmin):
inlines = [PropertyAdmin, PersonAdmin, CompanyAdmin, ContractAdmin]
admin.site.register(Property)
One solution to the problem can be, to create a custom form with fields from both the models and at the time of saving the values, first create the instance of Address model and then with that instance save your final Property model.

Categories

Resources