Sorry for the wording on the question if it isn't clear.
I am rather new to Django but not too new to Python. I have a Model in my Django Project, which keeps track of trailer locations. I then need to use another script to update this data. However, when running this script, the Dict that is suppose to be returned is not getting returned properly.
class TrailerLocation(models.Model):
trailer = models.OneToOneField(Trailer, on_delete=models.CASCADE)
locationCity = models.CharField(max_length=70, null=True, blank=True)
locationState = models.CharField(max_length=30, null=True, blank=True)
locationCountry = models.CharField(max_length=50, null=True, blank=True)
locationStatus = models.CharField(max_length=200, null=True, blank=True)
latitude = models.CharField(max_length=200, null=True, blank=True)
longitude = models.CharField(max_length=200, null=True, blank=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
# def __str__(self):
# return self.trailer
def createLocation(self, trailer):
trailerLocation = TrailerLocation(trailer=trailer)
trailerLocation.save()
def getLocations(self):
locDataDict = trailerLocations.run()
print(locDataDict)
for key in locDataDict.keys():
datalist = locDataDict[key]
self.updateLocation(key, datalist)
Related
In my project there is video module, related to this module user can add vodeo bookmarks,
class VideoBookmark(BaseModel, SoftDelete):
video = models.ForeignKey(Video, on_delete=models.CASCADE)
sort_order = models.PositiveSmallIntegerField(null=False, blank=False)
name = models.CharField(max_length=254, null=True, blank=True)
description = models.CharField(max_length=254, null=True, blank=True)
duration = models.DurationField(default=timedelta())
start_time = models.DurationField(default=timedelta())
end_time = models.DurationField(default=timedelta())
mobile_thumbnail_image = models.ImageField(upload_to='video_bookmark_mobile_thumbnail', height_field=None,
width_field=None, null=True, blank=True)
web_thumbnail_image = models.ImageField(upload_to='video_bookmark_web_thumbnail', height_field=None,
width_field=None, null=True, blank=True)
def __str__(self):
return str(self.name)
Bookmark3 is in the order of "2" and when trying to add bookmark2 in the order of "2" the order of bookmark3 should be changed to the order of "3".I don't know how to implement this, Can you suggest a way to implement this?
I am new to testing please help and thanks in advance
BackupRequestmodel
class BackupRequest(models.Model):
request_no = models.ForeignKey(AllRequest, on_delete=models.CASCADE)
requirement = models.CharField(max_length=49)
backup_type = models.CharField(max_length=49)
purpose = models.CharField(max_length=150, null=False)
department = models.ForeignKey(Department, on_delete=models.CASCADE)
location = models.ForeignKey(Location, on_delete=models.CASCADE)
def __str__(self):
return self.request_no.__str__()
How to write a unit test for this model the dependent models are given below
AllRequestModel
class AllRequest(models.Model):
form_type = models.ForeignKey(RequestType, related_name='requests', default=None,
on_delete=models.CASCADE)
request_no = models.AutoField(max_length=50, default=None, primary_key=True, )
status = models.IntegerField(default=0)
requested_by = models.CharField(max_length=50, blank=False, default='')
requested_on = models.DateTimeField(auto_now_add=True, editable=False)
updated_by = models.CharField(max_length=50, blank=False, default='')
updated_on = models.DateTimeField(auto_now_add=True, editable=True)
def __str__(self):
return str(self.request_no)
RequestTypeModel
class RequestType(models.Model):
name = models.CharField(max_length=150)
Fixed it. I was making a mistake. I was trying to pass the temporary instance of all_request() instead of saving it first using all_request.save().
My group and I are attempting to rewrite our website. We are keeping an Oracle database back-end, and rewriting the front-end with Python Django.
No problem connecting to Oracle, or accessing the datatables. Now I'm trying to add an Oracle view to the models.py, and I can't get it to work.
I googled this question with many results, so I know it is possible and apparently common, but I can't get it to work.
All I've done so far is add the class to models.py. We are using PhCharm as our IDE.
When I try to access the data using the Python Console, I can import the class, but when I try to load the QuerySet I get the error "Unable to get repr for " in the result window.
Does anyone see something I'm missing from my model?
Here is the model:
class LawBillRoleActionInfoView(LawBaseClass):
combo_id = models.AutoField(primary_key=True)
rltp_role_cd = models.CharField(max_length=2, blank=True)
bill_dft_no = models.CharField(max_length=6, blank=True)
session_id = models.IntegerField(blank=True, null=True)
ebrl_appl_seq = models.IntegerField(blank=True, null=True)
enty_id_seq = models.IntegerField(blank=True, null=True)
lst_nm = models.CharField(max_length=100, blank=True)
fst_nm = models.CharField(max_length=40, blank=True)
mi = models.CharField(max_length=1, blank=True)
entity = models.CharField(max_length=145, blank=True, null=True)
prtydist = models.CharField(max_length=11, blank=True, null=True)
blac_appl_seq = models.IntegerField(blank=None, null=True)
role_descr = models.CharField(max_length=80, blank=True)
shr_ttl = models.CharField(max_length=80, blank=True)
lmt_ind = models.CharField(max_length=1, blank=True)
bltp_bill_typ_cd = models.CharField(max_length=2, blank=True)
bill_no = models.IntegerField(blank=True, null=True)
actn_descr = models.CharField(max_length=80, blank=True)
actn_dt = models.DateField(blank=True, null=True)
sess_law_chpt_no = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'law_bill_role_action_info_vw'
unique_together = (('bill_dft_no', 'rltp_role_cd', 'session_id'),)
app_label = 'laws'
Here are the commands I am running from the Python Console:
from laws.models import LawBillRoleActionInfoView
bill_qs = LawBillRoleActionInfoView.objects.all()
Here is the LawBaseClass:
Base Class
class LawBaseClass(models.Model):
"""
Base class
"""
created_by = models.CharField(max_length=30, blank=True, null=True)
dtm_created = models.DateField(blank=True, null=True)
mod_by = models.CharField(max_length=30, blank=True, null=True)
dtm_mod = models.DateField(blank=True, null=True)
class Meta:
managed = False
abstract = True
I have a form where I can create and edit a group (a business really). Though I want to be able to say this business has many locations. I have the models written but the UI is giving me trouble.
I think mostly my question would be answered by how to update the Group model (creating/editing a group) and the GroupLocations model with a single form (adding an address as a new location if need be) so almost three models with a single form?
The models are:
class Address(models.Model):
city = models.CharField(max_length=50)
state = models.CharField(max_length=50)
zip_code = models.CharField(max_length=50)
address_line_one = models.CharField(max_length=50)
address_line_two = models.CharField(max_length=50, null=True, blank=True)
contact = models.CharField(max_length=50, null=True, blank=True)
phone = models.CharField(max_length=50)
fax = models.CharField(max_length=50, null=True, blank=True)
created_at=models.DateField(auto_now_add=True)
updated_at=models.DateField(auto_now=True)
def __str__(self):
return self.city
class Group(models.Model):
group_name = models.CharField(max_length=50)
group_contact= models.CharField(max_length=50)
tin = models.CharField(max_length=50)
npi =models.CharField(max_length=50)
notes = models.TextField(max_length = 255, null=True, blank=True)
billing_address = models.ForeignKey('Address', related_name = 'billing_address', on_delete=models.SET_NULL, null=True)
mailing_address = models.ForeignKey('Address', related_name = 'mailing_address', on_delete=models.SET_NULL, null=True, blank=True)
start_date = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True)
end_date = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True)
change_date = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True)
change_text = models.TextField(max_length = 255, null=True, blank=True)
term_comment = models.TextField(max_length = 255, null=True, blank=True)
group_phone=models.CharField(max_length=50)
group_fax = models.CharField(max_length=50)
group_term = models.ForeignKey(GroupTerm, on_delete=models.SET_NULL, null=True, blank=True) #quesiton is can a group be termed many times?
created_at=models.DateField(auto_now_add=True)
updated_at=models.DateField(auto_now=True)
#provider_location = models.ManyToManyField('ProviderLocations', through='GroupLocations')
def __str__(self):
return self.group_name
class GroupLocations(models.Model):
address = models.ForeignKey('Address', on_delete= models.SET_NULL, null=True)
group = models.ForeignKey('Group', on_delete=models.CASCADE)
doing_business_as = models.CharField(max_length = 255)
created_at=models.DateField(auto_now_add=True)
updated_at=models.DateField(auto_now=True)
def __str__(self):
return self.doing_business_as
I would love to (for this model and others) mimic how the admin handles users with the nice left and right multiple select boxes: (Though not sure how the add new location maybe a button to take you to another form that somehow knows its supposed to be added to the group as well?)
I realize this is a very large question I am looking for just mostly I think how to deal with several models on one form/template rails had a word for this (the name escapes me now) and not sure what the Django paradigm is.
I have a somewhat odd scenario. I am using a read only database that I have access through my property management software. They allow the user to define fields in there software. However they don't show up as specific fields in the database. The database has 2 tables related to them propuserdefined and propuserdefinedvalues. The propuserdefined contains all the info about the field(id, name, description) the propuserdiefinedvalues contains the values associated with the property. It has propid, userdefinedid, and value. My question is this, I need to access the value of propuserdefinedvalues where propid equals the propid in my for property in properties statement and the userdefinedid equals 49. How would I do this? Is it with a template tag?
Thanks in advance for your help.
Brandon
Here are my models.
class Propuserdefined(models.Model):
userdefinedid = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50L, blank=True)
type = models.IntegerField()
userid = models.IntegerField(null=True, blank=True)
updated = models.DateTimeField(null=True, blank=True)
datecreated = models.DateTimeField(null=True, blank=True)
combolist = models.TextField(blank=True)
class Meta:
db_table = 'propuserdefined'
class Propuserdefinedvalues(models.Model):
userdefinedid = models.IntegerField()
propid = models.IntegerField()
value = models.TextField(blank=True)
userid = models.IntegerField(null=True, blank=True)
updated = models.DateTimeField(null=True, blank=True)
datecreated = models.DateTimeField(null=True, blank=True)
class Meta:
db_table = 'propuserdefinedvalues'
class Property(models.Model):
propid = models.IntegerField(primary_key=True)
name = models.CharField(max_length=35L, blank=True)
shortname = models.CharField(max_length=6L, blank=True)
street1 = models.CharField(max_length=50L, blank=True)
street2 = models.CharField(max_length=50L, blank=True)
city = models.CharField(max_length=50L, blank=True)
state = models.CharField(max_length=2L, blank=True)
zip = models.CharField(max_length=50L, blank=True)
phone = models.CharField(max_length=21L, blank=True)
fax = models.CharField(max_length=50L, blank=True)
email = models.CharField(max_length=255L, blank=True)
manager = models.CharField(max_length=25L, blank=True)
billname1 = models.CharField(max_length=35L, blank=True)
billname2 = models.CharField(max_length=35L, blank=True)
billstreet1 = models.CharField(max_length=50L, blank=True)
billstreet2 = models.CharField(max_length=50L, blank=True)
billcity = models.CharField(max_length=50L, blank=True)
billstate = models.CharField(max_length=2L, blank=True)
billzip = models.CharField(max_length=50L, blank=True)
proptaxid = models.CharField(max_length=35L, blank=True)
rentchargetype = models.CharField(max_length=20L, blank=True)
lastpostdate = models.DateField(null=True, blank=True)
lastweeklypostdate = models.DateField(null=True, blank=True)
comments = models.CharField(max_length=25L, blank=True)
enablespeciallatecharge = models.IntegerField(null=True, blank=True)
fixedlatecharge = models.IntegerField(null=True, blank=True)
fixedlateamount = models.FloatField(null=True, blank=True)
fixedlaterentonly = models.IntegerField(null=True, blank=True)
percentlate = models.IntegerField(null=True, blank=True)
percentlateamount = models.FloatField(null=True, blank=True)
percentlatefullcharge = models.IntegerField(null=True, blank=True)
percentlaterentonly = models.IntegerField(null=True, blank=True)
perdaylate = models.IntegerField(null=True, blank=True)
perdaylateamount = models.FloatField(null=True, blank=True)
perdaylategrace = models.IntegerField(null=True, blank=True)
perdaylategracenum = models.IntegerField(null=True, blank=True)
perdatelatelimitamount = models.FloatField()
perdaylategracenonretro = models.IntegerField()
perdaylategraceexclweekends = models.IntegerField()
perdaylategraceexclholidays = models.IntegerField()
datecreated = models.DateTimeField(null=True, blank=True)
updated = models.DateTimeField(null=True, blank=True)
userid = models.IntegerField(null=True, blank=True)
logofile = models.CharField(max_length=255L, blank=True)
merchantid = models.CharField(max_length=255L, blank=True)
epaybankid = models.IntegerField()
epaylimit = models.FloatField()
epayenabled = models.IntegerField()
achconveniencefeeenabled = models.IntegerField()
ccconveniencefeeenabled = models.IntegerField()
rwaachconvenciencefeeenabled = models.IntegerField()
rwaccconveniencefeeenabled = models.IntegerField()
epayislimited = models.IntegerField()
epayusedefaults = models.IntegerField()
achconveniencefee = models.FloatField(null=True, blank=True)
ccconveniencefee = models.FloatField(null=True, blank=True)
rwaachconveniencefee = models.FloatField(null=True, blank=True)
rwaccconveniencefee = models.FloatField(null=True, blank=True)
epaychargetype = models.IntegerField()
epayamounttype = models.IntegerField()
epaysetamount = models.FloatField()
epaycustlimit = models.FloatField()
sqft = models.IntegerField()
lateminbalance = models.FloatField(null=True, blank=True)
defaultbank = models.IntegerField()
postday = models.IntegerField(null=True, blank=True)
active = models.IntegerField(null=True, blank=True)
iscommercial = models.IntegerField(null=True, blank=True)
assignedissueuserid = models.IntegerField(null=True, blank=True)
altname = Propuserdefinedvalues.objects.filter(userdefinedid=49)
class Meta:
db_table = 'property'
It sounds like you are attempting to do this in a Django template. You should instead be using Python code, because Django templates are not designed for this.
The Django models for the table also won't provide the nicest interface for accessing these properties. Instead you should create some functions on top of them. Alternatively you could write raw SQL that do joins across the two tables.
Using your models as they are (there are no ForeignKeys defined, so you can't use the ORM to follow relationships), you can get the details like this (if I understood your question correctly):
property = Property.objects.get(name='my_property_name') # or however you get the property
prop_user_defined_values = Propuserdefinedvalues.objects.filter(propid=property.id, userdefinedid=49)
However, this could be shorted if you changed the order of your models, and some of your fields to type ForiegnKey:
class Property(models.Model):
# ... rest truncated ...
class Propuserdefined(models.Model):
# ... rest truncated ...
class Propuserdefinedvalues(models.Model):
property = models.ForeignKey(Property, db_column='propid')
userdefined = models.ForeignKey(Propuserdefined, db_column='userdefinedid')
# ... rest truncated ...
This would let you do something like:
Propuserdefinedvalues.objects.filter(userdefined__name='my_name', property__name='my_property')
# or:
my_property = Property.objects.get(name='my_property')
Propuserdefinedvalues.objects.filter(userdefined__userdefinedid=49, property=my_property)
I suggest you read about Django's models here: https://docs.djangoproject.com/en/1.5/topics/db/models/ - they're quite easy to get right, even if you have pre-existing tables, as long as you know the relationships.
(Disclaimer: untested code! May be bugs ;))