How to pass value into another model based on conditional statement - django

I want to create a countdown timer target that will be added into date_target model, using override save method in models.py, the time will countdown differently based on it's variety.
import datetime
class CustomerVariety(models.Model):
variety = models.CharField(max_length=100, null=True)
def __str__(self):
return self.variety
class Customer(models.Model):
cardnumber = models.CharField(max_length=15, null=False)
name = models.CharField(max_length=100, null=False)
date_in = models.DateTimeField(auto_now_add=False, auto_now=False, blank=True,
null=True)
date_target = models.DateTimeField(auto_now_add=False, null=True)
variety = models.ForeignKey(CustomerVariety,
on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
if not self.pk:
if self.variety == "Priority Customer":
self.date_target = self.date_in + datetime.timedelta(days=3)
elif self.variety == "Secondary Customer":
self.date_target = self.date_in + datetime.timedelta(days=5)
super(Customer, self).save(*args, **kwargs)
when i pass value into form and save it, why date_target models doesn't full fill the value ?

Related

Django Admin, show inline based on slug

Have the following models
class FootballWebsite(models.Model):
"""Football service website."""
url = models.URLField, unique=True)
#football service
id = models.CharField(primary_key=True,
#is this domain blocked
blocked = models.BooleanField(default=False)
#is it online or offline
online = models.BooleanField(default=False)
updated = models.DateTimeField(auto_now=True, auto_now_add=True)
sub_categories = models.ForeignKey(SubCategory, default=1)
referral = models.TextField(blank=True)
mirror = models.ForeignKey('FootballWebsite', blank=True, null=True)
rank = models.PositiveIntegerField(default=0, blank=True, null=True)
screenshot = models.BooleanField(default=False)
class Meta:
"""Meta class."""
app_label = 'ahmia'
def __unicode__(self):
return u'%s' % (self.url)
"""The datetime when the football service was last seen online"""
try:
return self.footballwebsitestatus_set.filter(online=True).latest('time').time
except FootballWebsiteStatus.DoesNotExist:
return None
class FootballWebsiteDescription(models.Model):
"""Football service website description."""
about = models.ForeignKey(Footballebsite)
title = models.TextField(blank=True, null=True)
keywords = models.TextField(blank=True, null=True)
description = models.TextField(blank=True, null=True)
relation = models.URLField(blank=True, null=True)
subject = models.TextField(blank=True, null=True)
type = models.TextField(blank=True, null=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=True)
language = models.TextField(null=True, blank=True)
contactInformation = models.TextField(null=True, blank=True)
officialInfo = models.BooleanField(default=False)
slug = AutoSlugField(populate_from=['title'], allow_duplicates=True, null=True)
class Meta:
"""Meta class."""
app_label = 'ahmia'
def __unicode__(self):
return unicode(self.about.url)
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(FootballebsiteDescription, self).save(*args, **kwargs)
def __unicode__(self):
return u'%s' % (self.title)
I have a huge amount of links, and i would like to bulk assign them into a category or mark them as blocked based on identical title slug.
Managed to at least get a list of title_slugs with the code below, but the following step, i would like to get an inline list with all sites that have an identical title_slug and bulk assign those all in their a category
class FootballWebsiteInline(admin.TabularInline):
model = FootballWebsite
class FootballWebsiteDescriptionAdmin(admin.ModelAdmin):
list_display = ['show_slug']
def show_slug(self, obj):
return format_html("<a href='{url}'>{url}</a>", url=obj.slug)
inlines = [
FootballWebsiteInline,
]
Above code obviously doesn' t work, since the title slug which can appear many times is not a primary key.
Is it possible to get an inline list based on the title slug in this case which is not a primary key at all, or am i going about this the wrong way?
When possible at all, some further tweaking would be to group the identical title slugs

Problems trying to populate sub form question fields with answers

First time poster here. I have been working on my first project for the last few months. And have spent an absorbent amount of time trying to get this one piece to work. I am able to display my configuration which has a drop down for config type. Depending on the Type selected it will display a list of "Attributes" (Questions) and I would like to have my form so that I can pick a type of config and answer the questions that pertain to that config. The part I am getting stuck on is the line in my view attrib_obj = get_object_or_404(config_attribs, id=1). This will display the first answer correctly for evey config because I hard coded it to show the answer to 1 but it will then display the first answer to every question. I am struggling on how to make this variable to be the id for every question and not just the first one. Thank you for any help. Oh and since i am new to this i am not sure if i am saving my form correctly either. :) Visual of my Problem
My Model
class Configuration(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
config_type = models.ForeignKey('ConfigType', on_delete=models.PROTECT, null=True)
company = models.ForeignKey('companies.Company', on_delete=models.PROTECT, blank=True, null=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("configurations:configuration-update", kwargs={"id": self.id})
class ConfigType(models.Model):
name = models.CharField(max_length=50, blank=False)
inactive = models.BooleanField(default=False)
def __str__(self):
return self.name
class Attribute(models.Model):
config_type = models.ForeignKey('ConfigType', on_delete=models.PROTECT, null=True)
number = models.IntegerField()
attribute = models.CharField(max_length=100, blank=False)
input_type = models.ForeignKey('InputType', on_delete=models.PROTECT, null=True)
required = models.BooleanField(default=False)
#answer = models.ManyToManyField('Answer')
inactive = models.BooleanField(default=False)
def __str__(self):
return self.attribute
class InputType(models.Model):
name = models.CharField(max_length=100, blank=False)
def __str__(self):
return self.name
class Answer(models.Model):
configuration = models.ForeignKey('Configuration', on_delete=models.PROTECT)
attribute = models.ForeignKey('Attribute', on_delete=models.PROTECT, null=True)
text = models.CharField(max_length=100, blank=True, null=True)
checkbox = models.BooleanField(blank=True, null=True)
number = models.IntegerField(blank=True, null=True)
date = models.DateField(blank=True, null=True)
def __str__(self):
return self.configuration.name + " - Attribute #" + str(self.attribute.number)
My View
#login_required(login_url='login')
def configuration_update_view(request, id=id):
obj = get_object_or_404(Configuration, id=id)
config_attribs = obj.config_type.attribute_set.all()
attrib_obj = get_object_or_404(config_attribs, id=1)
config_answers = obj.answer_set.all()
answer_obj = get_object_or_404(config_answers, attribute=attrib_obj)
form = ConfigurationForm(request.POST or None, instance=obj)
attrib_form = ConfigAttribForm(request.POST or None, instance=answer_obj)
if form.is_valid():
form.save()
if attrib_form.is_valid():
attrib_form.save()
context = {
'form': form,
'attrib_form': attrib_form,
'config_answers': config_answers,
'config_attribs': config_attribs
}
return render(request, "configurations/configuration_detail.html", context)

Modifying save() in django model keeps looping

I'm trying to create a model based on the Google Map API.
If the object does not exists, I want to save the name, address, longitude, latitude and google place ID. Below is my code: However, when I run it, it goes into a loop and does stop checking Google Map. Can you tell me what is wrong?
class Place(models.Model):
name = models.CharField(max_length=200, null=True, blank=True)
address = models.CharField(max_length=200, null=True, blank=True)
logitude = models.CharField(max_length=20, null=True, blank=True)
latitude = models.CharField(max_length=20, null=True, blank=True)
id_google = models.CharField(max_length=50, null=True, blank=True)
date_created = models.DateTimeField(_('date created'), default=timezone.now)
date_modified = models.DateTimeField(_('date_modified'), auto_now=True)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
try:
place = Place.objects.get(name=self.name)
except Place.DoesNotExist:
gmaps = googlemaps.Client(key=settings.GOOGLE_MAPS_API_KEY)
geocode_result = gmaps.geocode(self.address)
place = Place(name=self.name,
address=geocode_result[0]['formatted_address'],
logitude=geocode_result[0]['geometry']['location']['lng'],
latitude=geocode_result[0]['geometry']['location']['lat'],
id_google=geocode_result[0]['place_id'],
)
place.save()
return place
You call place.save() in the save(..) method, and thus triggering another save. You probably can just edit the item inplace, and then save it by using a super().save() call:
class Place(models.Model):
# ...
def save(self, *args, **kwargs):
try:
place = Place.objects.get(name=self.name)
except Place.DoesNotExist:
gmaps = googlemaps.Client(key=settings.GOOGLE_MAPS_API_KEY)
geocode_result = gmaps.geocode(self.address)[0]
self.address = geocode_result['formatted_address']
location = geocode_result['geometry']['location']
self.logitude = location['lng']
self.latitude = location['lat']
self.id_google = geocode_result['place_id']
super().save(*args, **kwargs)

How can i get a object from a other function in django

I have two functions projectTimerStart to start the timer and projectTimerStop
i want to use the object which is created in projectTimerStart and i want to end the time when projectTimerStop , and this should be saved in a database
ps: Both the functions are not in class they are normal functions
def projectTimerStart(request, slug):
project_detail = Project.objects.get(slug=slug)
b = ProjectTimer(time_started=datetime.now(),
working_project=project_detail,
working_freelancer=request.user
)
b.save()
return HttpResponseRedirect(reverse('project_timer', kwargs=
{"slug":slug}))
def projectTimerStop(request, slug):
project_detail = Project.objects.get(slug=slug)
#i want something here super method or something
return HttpResponseRedirect(reverse('project_timer', kwargs=
{"slug": slug}))
models.py
class Project(models.Model):
project_title = models.CharField(max_length=100)
project_description = models.TextField()
created_by = models.ForeignKey(
User, on_delete=models.CASCADE, related_name='created')
assigned_to = models.ManyToManyField(
User, blank=True, related_name='assigned_by')
slug = models.SlugField()
hourly_budget = models.PositiveIntegerField(blank=True, null=True)
technologies = models.ManyToManyField(
Technologies, related_name='technologies_used')
time_posted = models.DateTimeField(auto_now_add=True)
request_id = models.ManyToManyField(
User, related_name='requested_by', blank=True)
def __str__(self):
return self.project_title
def save(self, *args, **kwargs):
self.slug = slugify(self.project_title)
super(Project, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('project_detail', kwargs={'slug': self.slug})
def get_timer_url(self):
return reverse('project_timer', kwargs={'slug': self.slug})
def round_datetime(dt):
minutes = round((dt.minute + float(dt.second) / 60) / 15) * 15 -
dt.minute
return dt + datetime.timedelta(minutes=minutes, seconds=-dt.second)
class ProjectTimer(models.Model):
time_started = models.DateTimeField()
time_ended = models.DateTimeField(blank=True, null=True)
working_project = models.ForeignKey(Project, on_delete=models.CASCADE)
working_freelancer = models.ForeignKey(
User, on_delete=models.CASCADE, blank=True, null=True)
If each of your project objects will have one and only project timer objects, you can add project_timer = models.OneToOneField(ProjectTimer) to your Project model and access to the project timer by using project_detail.project_timer.
If not, you need to know at least one feature of that project_timer in order to fetch it from database. Or you can iterate all of your ProjectTimer objects that belongs to that Project and select the appropriate one by:
models.py
class Project(models.Model):
# Some fields
project_timers = models.ManyToManyField(ProjectTimer)
views.py
def projectTimerStop(request, slug):
project_detail = Project.objects.get(slug=slug)
for pt in project_detail.project_timers.all():
if pt.some_field == "THIS IS CORRECT TIMER":
# Here is your project_detail
print(str(pt))
return HttpResponseRedirect(reverse('project_timer', kwargs=
{"slug": slug}))

How to create an inline formset for a reverse foreign key relationship

I have a Property Model as follows =
class Property(models.Model):
property_type = models.CharField(max_length=255, default='Apartment')
specifications = models.CharField(max_length=255, default='Basic')
built_up_area = models.FloatField(max_length=6, null=False, default=0)
total_area = models.FloatField(null=False, default=0)
number_of_bedrooms = models.CharField(max_length=3, default=1)
number_of_bathrooms = models.CharField(max_length=3, default=1)
number_of_parking_spaces = models.CharField(max_length=2, default=0)
address_line_one = models.CharField(max_length=255, null=False)
address_line_two = models.CharField(max_length=255, null=True, default=None)
connectivity = models.CharField(max_length=255, default=None, null=True)
neighborhood_quality = models.CharField(max_length=255, default=None,
null=True)
comments = models.CharField(max_length=255, default=None, null=True)
city = models.ForeignKey('City')
state = models.ForeignKey('State')
pin_code = models.ForeignKey('PinCode')
developer = models.ForeignKey('Developer', null=True, default=None)
owner = models.ForeignKey('Owner', null=True, default=None)
created_by = models.ForeignKey('custom_user.User')
project = models.ForeignKey('Project')
def __unicode__(self):
return self.property_type
class Meta:
verbose_name_plural = 'Properties'
And a City model as follows -
class City(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(City, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
Now I want to make a single form where I can enter the Property details and while entering the city, I can enter the name of the city instead of selecting from the dropdown list.
So how do I create the inline formset using the inlineformset_factory to create the form?
==EDIT==
I've tried to use the following code to create the formset
CityFormset = inlineformset_factory(City, Property,
fields=('city',),
extra=0,
min_num=1,
can_delete=False)
You've misunderstood what an inline formset is. It's for editing the "many" side of a one-to-many relationship: that is, given a parent model of City, you could edit inline the various Properties that belong to that city.
You don't want a formset at all to simply edit the single City that a property can belong to. Instead, override the city field within your Property form to be a TextField, and either create a new City or find an existing one in the clean_city method.
class PropertyForm(forms.ModelForm):
city = forms.TextField(required=True)
class Meta:
model = Property
exclude = ('city',)
def __init__(self, *args, **kwargs):
super(PropertyForm, self).__init__(*args, **kwargs)
if self.instance and not self.data:
self.initial['city'] = self.instance.city.name
def save(self, commit=True):
city_name = self.cleaned_data['city']
city, _ = City.objects.get_or_create(name=city_name)
instance = self.save(commit=False)
instance.city = city
if commit = True:
instance.save()
return instance