I have a material excelfile and it has different type of material.
Each category has start specific prefix like AB001 or BC001
I am planning to write a webservice for the request of this material stock codes.
But I need to import the my old database and i have stucked how can i implement this?
I have created a model in DJANGO like Materials, But i could not solve that if i import the my old excel file, the software how can understand the numbers will be correct.
It must be consecutively. Like ( AB001 , AB002, AC001,AC002,AZ001)
Thanks in advance!
Django ORM has an order_by() method. do you need something other than this?
The models meta class can allow for default order
models.py
class Material(models.Model):
material_stock_code = models.CharField(max_length=255)
class Meta:
ordering = ['material_stock_code']
The order_by() method can be used on any query
views.py
Material.objects.all().order_by('material_stock_code')
I would also recommend looking at django-rest-framework to make the API
Related
I'm building a django app that displays a database of employees and their salaries. It uses a postgres database. I'm struggling to figure out how to build a DetailView without using a primary key for the url. I think using a slug may be the solution but I'm not sure how to implement that and haven't had much luck searching online.
My Listview for the list of all companies:
class AllCompanies(FormMixin, generic.ListView):
template_name = 'AllCompanies.html'
form_class = SearchForm
model = bigdatabase
queryset = bigdatabase.objects.order_by().values('company').distinct()
context_object_name = 'companies'
Example Database snippet, bigdatabase:
Company EmployeeName Salary
Alpha Jim 100000
Alpha Tim 125000
Beta Bim 90000
My list view displays all unique company names as intended. However, I'm not sure how to proceed and build a detailview to display more info on each unique company. I'd like to show things like number of employees, median salary, etc.
I've done something similar by building a detailview for employees, but that relied upon using their primary key in the url since each employee is unique. Since I have many entries for many companies in my database, how would I build a detailview and accompanying url structure to support that?
Any advice or pointers as to move this along would be greatly appreciated.
You can add a slugfield to your bigdatabase model. Try using the autoslugfield and set it to the company name like:
from django_extensions.db.fields import AutoSlugField
class bigdatabase(models.Model):
company = Models.Charfield()
slug = models.AutoSlugField(populate_from=['company']
......
This makes sure you company name will automatically be translated for use in the url. E.g. when you have spaces in the company name, using str:company will translate in weird characters for the space. Using in your url translates this to a -.
Naming your model field slug makes sure that your detailview get the slug field by default. See docs.
Your views will then look something like:
class CompanyDetailView(DetailView):
model = bigdatabase
template_name = '#path_to_template'
slug_url_kwarg = 'slug' # I believe this is done correctly by default but in case you change the field name.
In your url you can do something like mentioned above:
path('company/<slug>', views.CompanyDetailView.as_view())
Happy coding!
Not sure that I understood correctly, but I thought you can use something like this:
path('company/<str:name>', views.company_detail_view)
I'm using Django to work on a web. I have created 2 apps: One for the clients to register, and add their data to the database, and a second app for users to access and see the interactive interface. The idea is to use the second app to get data from the clients in the database, and use it to show some information to the user.
My problem is that i don't understand how to make the second app to get the information from the database. Do i need to create the same models from the first app on model.py on the second one? Or how do i make the second app to use a Queryset to retrieve data from the database?
I don't know if is necesary to say that i'm using a MySql database.
You do not need to define the same models twice. In fact, you shouldn't for a number of reasons, like that the data should live in one place in your db (the table names get generated based on app name and model from migrations), and you should not repeat code (DRY).
You define the models in the application that they should belong (this is entirely a design decision). The migrations are created for the appropriate application.
Then, in the second application, you simply import the model that you wish to use from the first application and construct any query you like. Example:
app1/models.py
from django.db import models
class Node(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField()
body = models.TextField(blank=True)
app2/views.py
from django.views.generic.detail import DetailView
from app1.models import Node
class NodeView(DetailView):
model = Node
template_name = 'app2/index.html'
Is it possible get a queryset from a table in the app database that is NOT a model in the app?
If I have a table that is not a model named "cartable", conceptually, I want to do this:
myqueryset = cartable.objects.all()
Is there a relatively easy way to do this? Thanks!
If you want to access an existing table in your database that is not managed by your application, you can still create a class for it, and tell django to ignore it for migrations.
Just create a model and add the fields you need to access and then add a meta class to tell django to leave it alone.
class MyModel(model.Model):
class Meta:
managed = False
you can read about that at https://docs.djangoproject.com/en/1.9/ref/models/options/#managed
To do so you would need to create a class (not a model), with methods that use raw SQL. You should see more details here on how to do so: https://docs.djangoproject.com/en/1.9/topics/db/sql/#executing-custom-sql-directly
Please note that you will have to manually create the object with the right properties afterwards.
If you wanted to use Django ORM without the models, I don't think it is possible. You could however create a model that matches your db in a separate app and never create migrations for it to ensure you don't accidentally modify the DB.
Short answer is, "not really". Django QuerySet deals with model instances, so everything in QuerySet API is tied into models. Everything expects to return model instances, uses model fields etc.
That said, you should be able to create a model for an existing table. You will need to add db_table to the Meta, so Django knows where the table lives. If you have some indexing, you will need to make sure Django's idea of indexes is the same as the one in the database. indexed=True on fields and unique_together in Meta should help a lot with that.
I am trying to work out how to create a form for my Invoice model. It will include filling in the fields for my InvoiceLine model. This is possible via the admin interface, registering an inline object to the InvoiceAdmin model.
Any pointers on how to proceed for the same behavior on the front end?
The following are the models simplified only to include the model relationship and an arbitrary number
from django.db import models
class Invoice(models.Model):
number = models.IntegerField()
class InvoiceLine(models.Model):
invoice = models.ForeignKey(Invoice)
Do the lines and header of invoice via javascritp and send a json to server with the data, we work this way
You can use an inlineformset_factory. This wasn't my idea I found it on this blog post Django's Inlineformset_factory and You. Thanks Charles.
I have been dealing for a while with Django's authentication system and I just cannot understand why I have to go through this process Django doc! :
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User)
department = models.CharField(max_length=100)
... rather than simply extending the "User" class like this:
class Employee(User):
....
... and re-using all the code contained within. I have taken a look at articles like: b-list.org! , and I understand that the problem may be related with the automatic Django database management.
Is there a way in which I can automatically extend the User model without having to create an additional table in the database, so that Django modifies the current database table for me?
I tend to obey the fellas of the django
https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#extending-the-existing-user-model
Because only abstract models don't create tables in django and built-in user model is not