Hey guys I want to sort my many to many field and in that case I want to through. My code look something like this:
class SkirunRatePoint(models.Model):
latitude = models.DecimalField(u'lat', max_digits=10, decimal_places=6)
longitude = models.DecimalField(u'lng', max_digits=10, decimal_places=6)
elevation = models.DecimalField(max_digits=10, decimal_places=6, blank=True, null=True)
name = models.CharField(u'Name', max_length=200, blank=True, null=True)
class Meta:
verbose_name = u'Point'
verbose_name_plural = u'Points'
def __unicode__(self):
return unicode('{0} / {1}'.format(self.latitude, self.longitude))
class SkirunRoute(models.Model):
skirun = models.ForeignKey(Skirun, verbose_name=u'Path')
ratepoints = models.ManyToManyField(
SkirunRatePoint,
through="SkirunRatePointThrough",
verbose_name=u'Points',
blank=True,
)
class Meta:
verbose_name_plural = u'trasy z punktami'
def __unicode__(self):
return unicode(self.skirun)
class SkirunRatePointThrough(models.Model):
skirunroute = models.ForeignKey(SkirunRoute, related_name="skirun_route")
skirunratepoint = models.ForeignKey(SkirunRatePoint, related_name="skirun_rate_points")
order = models.IntegerField(
blank=True,
null=True,
)
Don't mind about indents, they are find on my pc.
Makemigrations is going fine, but when I try to migrate it throws me an error which says:
ValueError: Cannot alter field skirun.SkirunRoute.ratepoints into skirun.SkirunRoute.ratepoints - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)
Any ideas what might be the problem ?
Related
I have a model called Actual:
# Actual parts table
class Actual(models.Model):
vendor = models.ForeignKey(Vendor, on_delete=models.CASCADE, verbose_name="Vendor name", related_name="actualvendor")
number = models.CharField("External part number", max_length=32, unique=True, blank=True, null=True)
description = models.CharField(max_length=64, blank=True)
pq = models.DecimalField(max_digits=7, decimal_places=2, default=1)
mrrp = models.DecimalField(max_digits=10, decimal_places=2)
# Model metadata
class Meta:
unique_together = ["vendor", "number"]
verbose_name_plural = "actual external parts"
# Display below in admin
def __str__(self):
return f"{self.number}"
I also have another model called Offer:
class Offer(models.Model):
sync_id = models.ForeignKey(Sync, on_delete=models.CASCADE, verbose_name="Internal part number", related_name="part")
discount = models.DecimalField(max_digits=3, decimal_places=2, default=0)
moq = models.DecimalField(max_digits=4, decimal_places=2, default=1)
status = models.CharField(max_length=20, choices=OFFERSTATUS_CHOICES, default=1)
actual = models.OneToOneField(Actual, on_delete=models.CASCADE)
# Display something in admin
def __str__(self):
return f"Offer {self.id} for {self.sync_id}"
# Calculate the price
def price(self):
return self.actual.mrrp * (1-self.discount)
I am trying to calculate the 'price' in the 'Offer' model using 'mrrp' but 'mrrp' is from the 'Actual' model.
I am able to do so with the code I've attached but as you can see in the django admin, the 'actual' shows up as a field. I don't want it to appear as a field. I just want 'actual' to be a variable that is equal to the value of 'mrrp'. That way I can use it to calculate the price.
Is there another way to reference fields from another model? Surely fields aren't the only way?
I've trying to setup a model to be populated with a csv file, and to use one of the fields as a foreignkey:
First, I tried creating a model from an existing postgresql table using inspectdb, migrating and then change the type of the field I want to relate from charfield to foreignkey, but I got errors
Then I tried creating an empty model with the inspectdb description, then importing a csv in postgresql, and finally changing the field i want to foreignkey. In the admin site I can view the imported data and the relations, but when I query the model I got another error with the foreignkey field, and it wont let me migrate (cant recognize the charfield, it asks for integer)
-the csv column I want for a foreignkey is charfield, when i create the model before importing the csv i got an error, then checking the datatype in the postgres table is says integer, can it be set to charfield??
What workflow work best for importing a csv to a model and use one of the fields as a foreignkey (charfield type)??
Thanks
This is the model to be related to :
class D_Roles(models.Model):
predio = models.CharField(max_length=254)
dest = models.CharField(max_length=254)
dir = models.CharField(max_length=254)
rol = models.CharField(max_length=254)
vlr_tot = models.FloatField()
ub_x2 = models.FloatField()
ub_y2 = models.FloatField()
instrum = models.CharField(max_length=254)
codzona = models.CharField(max_length=254)
nomzona = models.CharField(max_length=254)
geom = models.MultiPointField(srid=32719)
def __str__(self):
return self.rol
class Meta():
verbose_name_plural = "Roles SII"
The models I want to populate with csv:
class Dom2015CertInf(models.Model):
id = models.CharField(primary_key=True, max_length=80)
nombre_archivo = models.CharField(max_length=180, blank=True, null=True)
derechos = models.CharField(max_length=120, blank=True, null=True)
dir_calle = models.CharField(max_length=120, blank=True, null=True)
dir_numero = models.CharField(max_length=120, blank=True, null=True)
fecha_certificado = models.CharField(max_length=50, blank=True, null=True)
numero_certificado = models.CharField(max_length=50, blank=True, null=True)
numero_solicitud = models.CharField(max_length=50, blank=True, null=True)
#Original field from inspectdb
#rol_sii = models.CharField(max_length=50, blank=True, null=True)
#FOREIGNKEY Changed after creating the model and importing the csv
rol_sii = models.ForeignKey(D_Roles, db_column='rol_sii', on_delete=models.CASCADE, default=0)
zona_prc = models.CharField(max_length=120, blank=True, null=True)
def __str__(self):
return str(self.numero_certificado)
class Meta:
managed = True
verbose_name_plural = "2015 Certificados Informaciones Previas"
ordering = ['numero_certificado']
I have a table that displays a list of "leads" which are rendered fine. There is also a related model which is called "Leadupdate" that is related to "lead" model that is used in the table. There is a many to one relationship from Leadupdate to lead with a foreign key. I want to display all the related updates for the individual "leads" in one of the updates column. There are several examples online for following forward relationship through foreign key but haven't found one for reverse yet. Here is one example of said relationship Accessor forward look up.
EDIT: Look up will be done on a Django-tables2 module instance table. I am not asking reverse look up on a model but doing it in context of Django-tables2.
Models.py:
class lead(models.Model):
slug = models.SlugField(unique=True,blank=True, null=True)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100, blank=True, null=True)
business_name = models.CharField(max_length=100,blank=True, null=True)
email = models.EmailField(max_length=75, blank=True, null=True)
phone_number = models.CharField(max_length=20, blank=True, null=True)
address = models.CharField(max_length=150, blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
state = models.CharField(max_length=10, blank=True, null=True)
zipcode = models.CharField(max_length=5, blank=True, null=True)
submission_date = models.DateTimeField(auto_now_add=True, blank=True)
assigned_to = models.ManyToManyField(Listing,blank=True, null=True, related_name="leads")
requested_software = models.CharField(max_length=50, blank=True, null=True)
type_of_business = models.CharField(max_length=30, choices=TYPE_OF_BUSINESS, default='Bar', blank=True, null=True)
time_frame = models.CharField(max_length=10, choices=TIME_FRAME, default='1')
comments = models.TextField(blank=True, null=True)
def __unicode__(self):
return self.business_name
#models.permalink
def get_absolute_url(self):
return('listing_detail', (),{'slug' :self.slug,})
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.business_name)
super(lead, self).save(*args, **kwargs)
class Leadupdate(models.Model):
CONFIDENCE_LEVEL = (
('HOT', 'HOT'),
('COLD', 'COLD'),
)
LEAD_VALUE = (
('1K3K', '1K-3K'),
('5K10K', '5K-10K'),
('10K20K', '10K-20K'),
('20K50K', '20K-50K'),
('50KUP', '5OK-UP'),
)
ESTIMATED_CLOSING = (
('1w4w', '1-4 Weeks'),
('1m3m', '1-3 Months'),
('3m6m', '3-6 Months'),
('6m+', '6+ Months'),
)
updatedate = models.DateTimeField(auto_now_add=True)
update = models.TextField(blank=True, null=True)
updatefrom = models.ForeignKey(Listing, related_name="update_from", blank=True, null=True)
lead = models.ForeignKey(lead, related_name="related_update",blank=True, null=True)
lead_confidence_level = models.CharField(max_length=10, choices=CONFIDENCE_LEVEL, default='COLD', blank=True, null=True)
estimated_lead_value = models.CharField(max_length=10, choices=LEAD_VALUE, default='1K3K', blank=True, null=True)
estimated_closing_frame = models.CharField(max_length=10, choices=ESTIMATED_CLOSING, default='1-4 Weeks', blank=True, null=True)
def __unicode__(self):
return u" %s - %s " % (self.update, self.updatedate)
Table:
class LeadTable(tables.Table):
business_name = tables.LinkColumn('lead-detail', args=[A('slug')])
updates = tables.Column(accessor='lead.related_update')
class Meta:
model = lead
fields = ("business_name","first_name", "last_name","number_of_pos","submission_date","updates")
attrs = {"class":"paleblue"}
A late answer, but here is what works for me in Django 1.8.6 with django-tables2 1.1.0 (based on Django-Tables2 Issue 156 and This answer). To access a one to many set of objects via a foreign key relation you need to just use the related_name in the accessor and then create a render method to produce what gets written to column cell. In that method you can then get all the foreign model objects and access their fields in a for loop.
class LeadTable(tables.Table):
business_name = tables.LinkColumn('lead-detail', args=[A('slug')])
updates = tables.Column(accessor='related_update')
def render_updates(self, value, table):
updates = ""
uFirst = True
updatesList = list(value.all())
for u in updatesList:
if not uFirst:
updates += ", "
else:
uFirst = False
updates += u.update
return updates
class Meta:
model = lead
fields = ("business_name","first_name", "last_name","number_of_pos","submission_date","updates")
attrs = {"class":"paleblue"}
according to django docs
in your views you can access them in this way (assuming lead_instance is an instance of lead class):
all_leadtables_for_lead = lead_instance.leadtable_set
a side note: use Capitalized names for classes (class Lead(models.Model):) in order to adhere to python PEP8 guidelines.
I have been working on a model for tags and am trying to avoid using contenttypes. I have couple questions related to ManyToManyField in django.
I have the following model
taggables/models.py
class Tag(models.Model):
tag_statuses = (
(u'P', _('Pending approval')),
(u'A', _('Approved')),
)
slug = models.SlugField()
created_at = models.DateTimeField(null=True, blank=True)
created_by = models.ForeignKey(User, related_name='tagged_item_created_by')
status = models.CharField(max_length=20, choices=tag_statuses)
site = models.ForeignKey(Site, default=settings.SITE_ID, related_name='tagged_item_site')
def __unicode__(self):
return self.slug
class TagI18n(models.Model):
tag = models.CharField(max_length=100)
descriptor = models.TextField(null=True, blank=True)
# i18n properties
item = models.ForeignKey(Tag)
language = models.CharField(max_length=6, choices=settings.LANGUAGES, default=settings.LANGUAGE_CODE)
class Meta:
unique_together = (("language", "item"))
def __unicode__(self):
return self.tag
I also have different apps around my project that uses tag model as many to many field. such as events for example
evetns/models.py
class Item(models.Model):
event_status_list = (
(u'P', _('Pending approval')),
(u'A', _('Approved')),
(u'R', _('Rejected')),
(u'S', _('Spam')),
)
published_at = models.DateTimeField(null=True, blank=True)
published_by = models.ForeignKey(User, null=True, blank=True, related_name='item_published_by')
updated_by = models.ForeignKey(User, null=True, blank=True, related_name='item_updated_by')
updated_at = models.DateTimeField(null=True, blank=True)
site = models.ForeignKey(Site, default=settings.SITE_ID, related_name='events_item_site')
event_slug = models.SlugField(null=True, blank=True)
# event timing
event_start_date = models.DateField()
event_start_time = models.TimeField(null=True, blank=True)
event_end_date = models.DateField()
event_end_time = models.TimeField(null=True, blank=True)
event_recurrent = models.BooleanField(default=False)
event_status = models.CharField(max_length=20, choices=event_status_list, default=u'P')
# relations
media = models.ManyToManyField(ImageFile, null=True, blank=True)
comments = models.ManyToManyField(Comment, null=True, blank=True)
votes = models.ManyToManyField(Vote, null=True, blank=True)
tags = models.ManyToManyField(Tag, null=True, blank=True)
audience = models.ManyToManyField(Audience, null=True, blank=True)
Now what am trying to do here is run a query to programmatically retrieve all the related models to Tag and then count how many a times a tag was used. Am sure I can do that with contenttypes (generic types) but I don't know how it will perform under heavy usage that's why I wanted to do the many to many fields.
If you are interested in the total number of usage ( aka reference count ) of a tag very often, I think you should store it in the database, example put one extra field to the Tag model, like
referencecount = models.IntegerField( default=0 )
Than in the appropriate places, ( example models .save() )you can increment or decrements it's value.
For your use case, the performance of generic wouldn't matter, because you need anyway to do N queries over 2N tables (one for each "taggable" model and one for each m2m join table, at least).
With the m2m approach, you should have the list of 'taggable' models stored somewhere, at least as a list of ('app_name', 'model') pairs. Then use ContentType (it's very performant) to get the actual model class or query directly from there:
counts = {}
for m in taggable_models:
ct = ContentType.get_by_natural_key(*m)
c = ct.model_class().objects.filter(tags=yourtag).distinct().count()
counts[ct.name] = c
I am working on a project and a student of Web Development.
I am making an application in Django. When a user creates a new trip with a form I've made, I want to add a map to that form so that users only have to click a point on a map in order to get coordinates to save to the database for that trip.
I want to save coordinates for every new trip so that I can render a map on each user's profile page that shows them markers of every trip they've taken that is in the database.
Please help!
Thank you
Here's some code:
models.py
class Trip (models.Model):
user = models.ForeignKey(User, on_delete = models.CASCADE, related_name='trips')
start_date = models.DateField()
end_date = models.DateField()
trail = models.CharField(max_length=300)
permit = models.URLField(blank=True, null=True)
completed = models.BooleanField(default=False)
location = models.CharField(blank=True, max_length=400)
lat = models.DecimalField(blank=True, null=True, max_digits=9, decimal_places=6)
lng = models.DecimalField(blank=True, null=True, max_digits=9, decimal_places=6)
created_at = models.DateField(auto_now_add =True)
def __str__ (self):
return self.trail
def total_days(self):
return (self.end_date - self.start_date).days
forms.py
class CreateTrip(forms.ModelForm):
start_date = forms.DateField(
widget=forms.TextInput(
attrs={'type': 'date'}
)
)
end_date = forms.DateField(
widget=forms.TextInput(
attrs={'type': 'date'}
)
)
class Meta():
model = models.Trip
fields = ('trail','location', 'permit', 'completed', 'start_date', 'end_date')
Sounds like to me you have a couple of many-to-one relationships you want to store in your database. My answer is based on this documentation:
https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_one/
You have Users that can go on many trips.
So you'd have a model called Trip that would look something like:
class Trip(models.Model):
name = models.CharField(max_length=100)
create_date = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User)
... whatever else ...
And your trips have points so you might want a model like this:
class Point(models.Model):
lat = models.DecimalField(blank=True, null=True, max_digits=9, decimal_places=6)
lng = models.DecimalField(blank=True, null=True, max_digits=9, decimal_places=6)
order = models.IntegerField() # ordering for the points.
create_date = models.DateTimeField(auto_now_add=True)
trip = models.ForeignKey(Trip)
... whatever else ...