I have a Django website and part of it lists data in rows with the primary key in one of the columns. This works great except for when I have separate users. I'm using foreign keys in each model to separate the different user's data. My problem is that the data for each user has to have a number in the column that increments numerically and does not have any spacing, for instance, 1,2,3,5 is bad. If User1 is uploading data and halfway through User2 uploads a row of data then if I use the primary key the numbers will not be in numerical order for each user and will read 1,2,3,5 for User1 and 4 for User2. I tried forloop counter but I need the numbers all the be assigned to the row and not change if one is deleted. Ive been at this for 2 weeks and am having a really hard time describing my problem. I think I need somesort of Custom User Primary Key, a primary key for each user. Any help at this point is greatly appreciated, Thanks!
I finally figured it out after about a week.
models.py:
class Sheet_Building(models.Model):
user = models.ForeignKey(User, default=True, related_name="Building", on_delete=models.PROTECT)
count_building = models.IntegerField(blank=True, null=True)
date = models.DateField(blank=True, null=True, verbose_name='Inspection Date')
time = models.TimeField(blank=True, null=True, verbose_name='Inspection Time')
inspection_type = models.CharField(max_length=16, choices=INSPECTION_TYPE_BI, blank=True, null=True, verbose_name='Inspection Type')
flname = models.CharField(max_length=25, blank=True, null=True, verbose_name='Inspector')
report_date = models.DateField(blank=True, null=True, verbose_name='Report Date')
department = models.CharField(max_length=29, choices=DEPARTMENT_BI, blank=True, null=True, verbose_name='Department')
responsible_name = models.CharField(max_length=25, blank=True, null=True, verbose_name='Responsible Person')
building_address = models.CharField(max_length=52, choices=BUILDING_ADDRESS, blank=True, null=True, verbose_name='Building and Address')
floor = models.CharField(max_length=8, choices=FLOOR_LEVEL_BI, blank=True, null=True, verbose_name='Floor / Level')
room = models.CharField(max_length=35, blank=True, null=True, verbose_name='Area / Room')
location = models.CharField(max_length=10, choices=LOCATION_BI, blank=True, null=True, verbose_name='Location')
priority = models.IntegerField(blank=True, null=True, verbose_name='Priority')
hazard_level = models.CharField(max_length=20, choices=HAZARD_LEVEL_BI, blank=True, null=True, verbose_name='Hazard Level')
concern = models.CharField(max_length=31, choices=CONCERN_BI, blank=True, null=True, verbose_name='Concern')
codes = models.CharField(max_length=51, choices=CODES_BI, blank=True, null=True, verbose_name='Element and Code')
correction = models.TextField(max_length=160, blank=True, null=True, verbose_name='Corrective Action')
image = models.ImageField(blank=True, null=True, verbose_name='Image', upload_to='gallery')
notes = models.TextField(max_length=500, blank=True, null=True, verbose_name="Inspector's note")
class Meta:
ordering = ['-pk']
def __str__(self):
return self.flname or 'None'
def get_absolute_url(self):
return reverse('list_building')
view.py:
def adddata_building(response):
if response.method == 'POST':
form = SheetForm_Building(response.POST, response.FILES)
if form.is_valid():
instance = form.save(commit=False)
instance.user = response.user #User
if Sheet_Building.objects.filter(user=response.user).values_list('count_building'):
instance.count_building = Sheet_Building.objects.filter(user=response.user).aggregate(count_building=Max('count_building'))['count_building'] + 1 #Count
else:
instance.count_building = 1
instance.save()
response.user.Building.add(instance)
return redirect('list_building')
else:
form = SheetForm_Building()
return render(response, 'sheets/add_data/add_data_building.html', {'form': form})
HTML:
{% for post in object_list %}
{{ post.count_building }}
{% enfor %}
Related
How do add new record every time I update the screen? instead it updates the existing record.?
Here are the models.
class Dealer(models.Model):
city = models.CharField('City', max_length=30, blank=True, null=True)
contact_name = models.CharField('Contact name', max_length=250, blank=True, null=True)
dealer_id = models.CharField('dealer_id', max_length=50, primary_key=True)
dealership_name = models.CharField('Dealership name', max_length=100, blank=True, null=True)
dsr = models.CharField('DSR', max_length=50, blank=True, null=True)
email = models.EmailField('Email', blank=True, null=True)
fax = models.CharField('Fax', max_length=20, blank=True, null=True)
dealer_notes = models.TextField('notes', blank=True, null=True)
phone = models.CharField('Phone', max_length=20, blank=True, null=True)
rating = models.CharField('Rating', max_length=20, blank=True, null=True)
state = models.CharField('State', max_length=5, blank=True, null=True)
street = models.CharField('Street', max_length=60, blank=True, null=True)
zip = models.CharField('Zip', max_length=10, blank=True, null=True)
def __str__(self):
return self.dealer_id
class Activity(models.Model):
dealer_id = models.ForeignKey(Dealer, on_delete=models.CASCADE)
activity_number = models.PositiveIntegerField(default=1)
date_created = models.DateField('date created', blank=True, null=True)
created_by = models.CharField('created_by', max_length=50, blank=True, null=True)
type = models.CharField('state', max_length=50, blank=True, null=True)
activity_notes = models.TextField(max_length=500, blank=True, null=True)
def __str__(self):
return f"{self.dealer_id} {self.activity_number}"
When it lands in dealer notes page it will get the details from the user to enter model fields and save.For each record there can be many number of activity notes. But when I tried to add a new note it updates the existing one, i want it create a new record.
def create_notes(request, dealer_id):
item, _ = Activity.objects.get_or_create(dealer_id_id=dealer_id)
form = ActivityForm(instance=item)
if request.method == 'POST':
form = ActivityForm(request.POST, instance=item)
if form.is_valid():
try:
form.save()
except:
pass
else:
return redirect('dealernotes/' + request.post.get('dealer_id_id'))
return render(request,
'dealernotes.html',
{'form': item,},
)
Can you help to create a new note every time? by activity number i should be able to update as well.
urls:
path('dealernotes/<str:dealer_id>?/', views.update_notes, name='dealernotes')
How do I fix this issue?
Don't use get_or_create and instead just use create
item = Activity.objects.create(dealer_id_id=dealer_id)
Edit 2 (scraping Edit1)
Here, Try to restructure it so that Activity is only created when you submit the form.
Note: We're changing the template, don't forget about that
def create_notes(request, dealer_id):
if request.method == 'POST':
form = ActivityForm(request.POST)
if form.is_valid():
try:
form.save()
except:
pass
else:
# debugging error:
print(form.errors.as_data())
return redirect('dealernotes/' + request.post.get('dealer_id_id'))
dealerObj = Dealer.objects.get(pk=dealer_id)
form = ActivityForm(initial={'dealer_id' : dealerObj})
data = {
'form': form,
}
return render(request, 'dealernotes.html', data)
Template
We're going to replace the PK input with a dealer_id, that way the form is valid on the post
<form action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
I have a Django website and one of my models has an integer field. Does anyone know of a way to retrieve the largest value currently in that field into the view? Thanks!
I finally figured it out after about a week. Its close to a combination of both SELECT MAX and aggregation
models.py:
class Sheet_Building(models.Model):
user = models.ForeignKey(User, default=True, related_name="Building", on_delete=models.PROTECT)
count_building = models.IntegerField(blank=True, null=True)
date = models.DateField(blank=True, null=True, verbose_name='Inspection Date')
time = models.TimeField(blank=True, null=True, verbose_name='Inspection Time')
inspection_type = models.CharField(max_length=16, choices=INSPECTION_TYPE_BI, blank=True, null=True, verbose_name='Inspection Type')
flname = models.CharField(max_length=25, blank=True, null=True, verbose_name='Inspector')
report_date = models.DateField(blank=True, null=True, verbose_name='Report Date')
department = models.CharField(max_length=29, choices=DEPARTMENT_BI, blank=True, null=True, verbose_name='Department')
responsible_name = models.CharField(max_length=25, blank=True, null=True, verbose_name='Responsible Person')
building_address = models.CharField(max_length=52, choices=BUILDING_ADDRESS, blank=True, null=True, verbose_name='Building and Address')
floor = models.CharField(max_length=8, choices=FLOOR_LEVEL_BI, blank=True, null=True, verbose_name='Floor / Level')
room = models.CharField(max_length=35, blank=True, null=True, verbose_name='Area / Room')
location = models.CharField(max_length=10, choices=LOCATION_BI, blank=True, null=True, verbose_name='Location')
priority = models.IntegerField(blank=True, null=True, verbose_name='Priority')
hazard_level = models.CharField(max_length=20, choices=HAZARD_LEVEL_BI, blank=True, null=True, verbose_name='Hazard Level')
concern = models.CharField(max_length=31, choices=CONCERN_BI, blank=True, null=True, verbose_name='Concern')
codes = models.CharField(max_length=51, choices=CODES_BI, blank=True, null=True, verbose_name='Element and Code')
correction = models.TextField(max_length=160, blank=True, null=True, verbose_name='Corrective Action')
image = models.ImageField(blank=True, null=True, verbose_name='Image', upload_to='gallery')
notes = models.TextField(max_length=500, blank=True, null=True, verbose_name="Inspector's note")
class Meta:
ordering = ['-pk']
def __str__(self):
return self.flname or 'None'
def get_absolute_url(self):
return reverse('list_building')
view.py:
def adddata_building(response):
if response.method == 'POST':
form = SheetForm_Building(response.POST, response.FILES)
if form.is_valid():
instance = form.save(commit=False)
instance.user = response.user #User
if Sheet_Building.objects.filter(user=response.user).values_list('count_building'):
instance.count_building = Sheet_Building.objects.filter(user=response.user).aggregate(count_building=Max('count_building'))['count_building'] + 1 #Count
else:
instance.count_building = 1
instance.save()
response.user.Building.add(instance)
return redirect('list_building')
else:
form = SheetForm_Building()
return render(response, 'sheets/add_data/add_data_building.html', {'form': form})
HTML:
{% for post in object_list %}
{{ post.count_building }}
{% enfor %}
I am trying to create cart using django but i am getting this error. while I try to check that the user is authenticated or no i used customer = request.user.customer but it says user has no attribute customer
Here is my views.py
def cart(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created = OrderModel.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
else:
items = []
context = {}
return render(request, 'Home/cart.html', context)
here is my models.py
class CustomerModel(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, null=True, blank=True, default='')
customer_name = models.CharField(max_length=1000, null=True)
customer_phone = models.CharField(max_length=20, null=True)
def __str__(self):
return self.customer_name
class OrderModel(models.Model):
customer = models.ForeignKey(CustomerModel, on_delete=models.SET_NULL, null=True, blank=True)
order_date = models.DateTimeField(auto_now_add=True)
complete = models.BooleanField(default=False,null=True,blank=True)
transaction_id = models.CharField(max_length=1000, null=True)
def __str__(self):
return str(self.id)
class OrderItem(models.Model):
product = models.ForeignKey(ProductModel, on_delete=models.SET_NULL, null=True, blank=True)
order = models.ForeignKey(OrderModel, on_delete=models.SET_NULL, null=True, blank=True)
quantity = models.IntegerField(default=0, null=True, blank=True)
date_added = models.DateTimeField(auto_now_add=True)
class Address(models.Model):
customer = models.ForeignKey(CustomerModel, on_delete=models.SET_NULL, null=True, blank=True)
order = models.ForeignKey(OrderModel, on_delete=models.SET_NULL, null=True, blank=True)
address = models.CharField(max_length=10000)
city = models.CharField(max_length=1000)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.address
I am stuck here and cant understand what to do.
I think changing the line customer = request.user.customer to customer = request.user.customermodel may solve your problem. If you want to use customer = request.user.customer add related name to your CustomerModel's field:
user = models.OneToOneField(User, on_delete = models.CASCADE, null=True, blank=True, default='', related_name='customer')
Note: Make sure that your user object has a related profile.
For example add an extra condition to your codes like following:
if hasattr(request.user, 'customer'): # If you have related name otherwise use customermodel
customer = request.user.customer
else:
# Return a proper message here
Because if your user object has no related profile this line of code will raise RelatedObjectDoesNotExist error type.
For the user field of the CustomerModel, you must set "related_name" and "related_query_name" to "customer":
user = models.OneToOneField(User, on_delete = models.CASCADE, null=True, blank=True, default='', related_name='customer', related_query_name='customer')
You have to set the "related_name" parameter in your CustomerModel customer field for reverse access
user = models.OneToOneField(User, related_name="user", on_delete = models.CASCADE, null=True, blank=True, default='')
if you don't set the related name django will generate field name + "_set" for the access (user_set in your example)
I have a User model that has 20 count field:
class User(models.Model):
username = models.CharField(max_length=16)
password = models.CharField(max_length=40) # sha1
real_name = models.CharField(max_length=12, null=True,blank=True)
phone = models.CharField( max_length=11)
email = models.EmailField(blank=True, null=True )
qq = models.CharField(max_length=10, null=True, blank=True)
address = models.CharField(max_length=64, blank=True, null=True)
id_card = models.CharField(blank=True, null=True, max_length=18, validators=[RegexValidator(regex='^.{18}$', message='身份证长度必须为18', code='nomatch')])
id_card_img_front = models.CharField(max_length=256, blank=True, null=True)
id_card_img_back = models.CharField(max_length=256, blank=True, null=True)
nickname = models.CharField(max_length=16, blank=True, null=True)
profile = models.CharField(max_length=256, blank=True, null=True, default=' ')
usertype = models.ForeignKey(to='UserType', default=1, blank=True)
user_c_type = models.CharField(max_length=4, null=True)
fixed_phone = models.CharField(max_length=16, null=True)
fax = models.CharField(max_length=16, null=True)
main_bussiness = models.CharField(max_length=16, null=True)
main_industry = models.CharField(max_length=16, null=True)
company_name = models.CharField(max_length=32, null=True)
company_address = models.CharField(max_length=32, null=True)
province = models.CharField(max_length=32, null=True, default="--省--")
town = models.CharField(max_length=32, null=True, default="--市--") # 省市县
country_level = models.CharField(max_length=32, null=True, default="--县--")
ctime = models.DateTimeField(auto_now_add=True)
uptime = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=1, null=True, default=1)
And in my Django project, I want to use Paginator to realize paging for user list.
Because the count of my user in my database is no more than 10,000 rows.
So, whether I can get all of the user in my database then paginate them?
user_all = models.User.objects.all()
paginator = Paginator(user_all, 10)
try:
user_list_page = paginator.page(page_number)
except PageNotAnInteger:
user_list_page = paginator.page(1)
except EmptyPage:
user_list_page = paginator.page(paginator.num_pages)
And I don't know whether this method(query out all the rows of user data) is inefficiency.
Or, how to weight the balance of query out the users from database? For a positive limit numbers ( over the count I should change paginate method, less than that I can use my page method )?
Or is there a better method to paginate my users?
The generic ListView brings a paginator. You should make sure that your queryset is sorted, otherwise the pages might repeat some of the objects (that is not a Django issue - paging without a sort order is just not practical).
https://docs.djangoproject.com/en/1.11/ref/class-based-views/generic-display/#listview
from django.views.generic.list import ListView
class UserListView(ListView):
template = 'my_user_list_template.html'
model = User # not django.contrib.User but your's
paginate_by = 10
def get_queryset(self):
return super().get_queryset().order_by('realname', 'pk')
The template will contain all context data required to render pagination, and the max 10 objects for the current page.
There are 3rd party modules like pure_pagination that extend the pagination to allow for other GET parameters in the pagination links: https://github.com/jamespacileo/django-pure-pagination
More links:
https://docs.djangoproject.com/en/1.11/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin.paginate_by
See also on the same page: paginate_orphans, paginator_class etc.
I have 2 models that are connected.
Model 1 is userprofiles and model 2 is events
each user can have multiple events... each event can be active or deactivated.
I have a for loop in the template showing all the userprofiles... but I also want to show how many active events each user has.
today = datetime.now().strftime('%Y-%m-%d')
perm = Permission.objects.get(codename='chef_user')
user_profiles = User.objects.filter(profile__user_profile_active='y').filter(is_active=True).filter(Q(groups__permissions=perm) | Q(user_permissions=perm)).distinct()[:6]
in the template I have my loop
{% for kitchen_list in user_profiles %}
-- CODE --
{{ kitchen_list.events_set.count }} <--- WHERE I AM TRYING TO SHOW ACTIVE EVENTS COUNT
--CODE--
{% endfor %}
my models:
class UserProfile(models.Model):
EVENT_TYPE = (('0', "Apartment"), ('1', "Home"),)
CURRENCY = (('R', "Rand"),)
OPTIONS = (('y', "Yes"),('n', "No"),)
OPTIONS_USER = (('y', "Yes"), ('n', "No"),('p', "Pending"),)
user = models.OneToOneField(User, related_name='profile')
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,12}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
phone = models.CharField(validators=[phone_regex], max_length=12, default='', blank=True) # validators should be a list
currency = models.CharField(max_length=50, blank=True, default='R', choices=CURRENCY)
bookingpermissions = models.CharField(max_length=100, blank=True, default='n', choices=OPTIONS)
title = models.CharField(max_length=50, default='', blank=True)
bio = models.TextField(max_length=500, default='', blank=True)
city = models.CharField(max_length=100, default='', blank=True)
longitude = models.CharField(max_length=100, default='na', blank=True)
latitude = models.CharField(max_length=100, default='na', blank=True)
image = ResizedImageField(size=[400, 400], crop=['middle', 'center'], upload_to='userprofile/static/images', default='', blank=True)
kitchen_type = models.CharField(max_length=50, blank=True, default='a', choices=EVENT_TYPE)
user_profile_active = models.CharField(max_length=1, default='n', blank=True, choices=OPTIONS_USER)
class Events(models.Model):
ACTIVE = (('d', "Deactivated"), ('e', "Expired"), ('a', "Active"), ('b', "Drafts"),)
ALCOHOL = (('0','No bring own alcohol'),('1','There will be complimentary wine pairing'))
user = models.ForeignKey(User, on_delete=models.CASCADE)
active = models.CharField(max_length=1, default='b', choices=ACTIVE)
title = models.CharField(max_length=50, blank=True, default='')
description = models.TextField(max_length=500, blank=True, default='')
date = models.DateField()
time = models.TimeField()
price = models.CharField(max_length=240, blank=True, default='')
seats = models.IntegerField()
alcohol_choice = models.CharField(max_length=1, default='n' ,choices=ALCOHOL)
starter = models.TextField(max_length=350, blank=True, default='')
main_menu = models.TextField(max_length=350, blank=True, default='')
dessert = models.TextField(max_length=350, blank=True, default='')
notes = models.TextField(max_length=350, blank=True, default='')
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.title
you can try use model propery
in model:
class UserProfile(models.Model):
# You code here
#property
def active_events(self):
return self.user.events_set.filter(active='a').count()
in template:
{{ kitchen_list.userprofile.active_events }}
Rather do it in the views.py instead of the template to count then pass the values to the template. Assuming you hva e set up the foreign relations well in the models then in the views do the following:
user_profiles = User.objects.filter(profile__user_profile_active='y').filter(is_active=True)
counts = user_profiles.events_set.count()
Then pass that as a context.
Or you can try creating a method in your model UserProfile:
class UserProfile:
....
def events(self):
return self.events_set.count()