A given user can be in zero or more of the following categories:
floor staff
managers
owners
If the user is floor staff they require extra attributes (e.g. seniority and staffType).
My initial attempt looked something like this:
Create a Group for each category (this seems to make sense since each type of user should have different permissions within the site)
Create a FloorStaff model with the required extra attributes and a 1-to-1 relationship with User
Handle User creation and FloorStaff creation separately (i.e. using separate forms, accessed separately). The form to create FloorStaff adds the User to the floor staff group automatically.
While this works, it is not the most intuitive way to add a new floor staff employee to the site. From a manager's perspective, filling out 2 forms to add one employee makes little sense. That is, if I hire someone new, I should be able to fill out a "new employee" form that includes adding said employee to the correct categor(y/ies) at the same time as adding their basic info, and, if that employee is to be floor staff then the extra attributes should be added as well.
I've been toying with creating a custom user model that includes the extra attributes, but this feels wrong given that not all Users are FloorStaff.
At this point all I can think to do is to build a custom view and HTML form, but that too seems to go against the Django way of thinking.
What is the most "Djangoific" way to do this?
I would recommend making seniority and staffType as optional fields, and then create a custom model admin that enforces providing those fields when floor staff is true
Related
I am looking for a way to have a multiple choice field populated with choices made by a user. For example, they could have the following 3 entries: Yes, No, Unsure. I want a way to be translate this to a model.
I understand this can be done with pre-defined options using a CharField, or ChoiceField, but I haven't seen anything with "dynamic" data, such as user-generated data.
I think you shouldn't use choice fields in this case, because everytime a user creates a new option, you'll have to run a new migration.
Maybe you can create a UserOption model and create a new obj everytime a user creates a new option. Then fetch all the options the user has created when he needs to choose between them.
You can't apply user choices to a DB table. However, you could store a user's choices somewhere, such as a User's Profile, and use them in a dynamically constructed form with a ChoiceField to constrain what he can put into a particular database column / model field.
Could be a PITA if he decides to delete a choice and then wants to edit the (now invalid) data in one of his records.
I'm working on a Django app for keeping track of collections (coins, cards, gems, stamps, cars, whatever). You can have multiple collections, each collection can have sets (Pirates cards, Cardinals cards, etc.) and then of course the individual items in each collection/set. Each item can contain multiple pictures, a name, and description, but here's where I'm unsure how to proceed. Each collection will need it's own set of values, or fields, that the user will need to determine (condition, dimensions in the appropriate units, coin thickness, model number, etc). How can I make custom fields such that the user can name the field and choose the input type (text, numbers, dropdown w/choices) and those fields will show up to be entered on each item within that collection?
This would be called an Entity-Attribute-Value (EAV) model and it is quite tricky to implement in the way you want. You have to anticipate all sorts of issues with user input, how to validate field types, what happens when the user wants to change fields, etc. I would start by reading the issues raised in that question and think about ways that you could modify your schema to avoid letting users define their own metadata at runtime. Are there some fields that could be common to all collections (like condition, dimensions, model number)? How tolerant do you want to be of data type issues, and will users be allowed to change field types after creation?
The more thought you put into implementation, the more issues you can avoid down the road.
I am new to Django and am working through some models on a project right now.
Is there a decent way to set the following up?
Using a model that ties to the auth_user model (UserAccount), I prompt users to select the type of account they have (Ex: Vendor or Buyer).
This part works fine... but I want to build the site experience based off of their account type. How would I create VendorProfile or BuyerProfile models (with differing data) based upon that option?
I might be over analyzing this, but basically I do not want any profiles to be linked to both types of data. Is that something I should just control with views, or is there a good way to lock my models and prevent duplicate information? Let me know if you think this is too broad, but I'm still on the conceptual level.
So:
auth_user model
UserAccount model:
user: one to one (User)
account_type: 'V' or 'B'
VendorProfile(?)
user: one to one (UserAccount where type='v')?
company name:
vendor code:
BuyerProfile(?)
user: one to one (UserAccount where type='b')?
favorite color:
pet's name:
yes you can do that. for that you need to change a small thing in the auth table of django and that is you need to add a column account_type where the V or B will be stored. Hope you can find it how to do it.
next is create 2 models for vendor and buyer. make a onetoonefield with the user table for storing the id of the user, in this models keep the extra information of the vendor or buyer you want to store.
hope it helps
I'm making a web site for a friend for a small business, and for each user, I want them to be able to access their orders by number which starts from 1 for each user, but in the backend this should be a global numbering. So for each user, their first order will be at /orders/1/ and so on. Is there a consensus on how this should be achieved in general? Way I see it, I can do this 2 ways:
Store the number in another column in the orders table. I'd prefer not to do this because I'm not entirely sure how to handle deletions without going through and updating all the records of the user. If someone knows the edge cases I need to handle, I might go with this.
OR
For every queryset I make when getting the orders page for each user I handle the numbering, benefit of this is that it will always give the correct numbering, especially if I just do it in the template. Right now this seems easier, but I have a feeling this would give rise to problems in the future. Main problem I see is I'm not sure how to make it link to the correct url without the primary key being in that url.
I recommend you to store MyUser in a separate app, say accounts
class MyUser(BaseUser):
# extra fields
And store Order in a separate app, say order
from accounts.models import MyUser
class Order(models.Model):
user = models.ForeignKey(MyUser)
order_num = models.IntegerField()
# other fields
keep update this order_num by the count of orders the user has made.
to get the count,
count = Order.objects.filter(user==request.user).count()
I'm currently implementing a solution using django admin, it allows users to define in the db a product, and then custom attributes and details, more details may be aggregated by a common attribute, this allows me to query with ajax a custom view that returns some JSON data to build automagically the form fields that I need directly in the same formset view (manipulating the DOM).
The current DB design follows this schema:
Catalog(name, description, photo)
Product(rel_catalog, name, base_price, photo, manufacturer_email)
ProductDetail(rel_product, rel_attribute, percentage_price, fixed_price)
ProductAttribute(rel_product, name, description)
As you may see I have a catalog, where there can be more products, a lot of details per product, aggregated by attributes. Then I simple show by default the Catalog, then the select with all available products for that catalog, then, choosing the right Product, I obtain the complete form (each row has a label with ProductAttribute.name and a select with related ProductDetail).
All works pretty dam good, but I also need to store this references in the DB when someone completes the form (making an order with choosen products). This forms are displayed as StackedInline (the ModelAdmin is for the Order).
I don't know how many options there may be per product so I was thinking to use this design to track orders:
Order(customer, status, notes, tot_price, inserted_by)
OrderItem(rel_order, catalog, product, unit_price)
But I don't know how to store the dynamic added inputs...
I was thiking to implement OrderItemProperty(rel_orderitem, rel_productdetail, rel_productattribute) to store each single input... but how do I loop over this unknown fields?
Maybe do you suggest a better design?
If you need more code just ask for it and I'll reply with a pastebin link.
Thankyou.
Finally I got a working solution,
I've created a custom view, overriding the default "add/" view, this way I can customize whatever I want to and I can read the POST data handling each validation, putting then the data in the right model.